Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether product of \'n\' numbers is even or odd in Python
In this article, we explore different methods to check whether the product of n numbers is even or odd. A number that is divisible by 2 is known as an even number, otherwise it is an odd number.
For example, 14 and 12 are two even numbers, their product 168 is even. Numbers 9 and 5 are odd, their product 45 is odd. Consider one even number 2 and one odd number 3 − their product 6 is even.
Mathematical Facts
Understanding these mathematical rules helps us determine if a product is even or odd −
- The product of two even numbers is always even
- The product of two odd numbers is always odd
- The product of one even and one odd number is always even
Key insight: If any number in the set is even, the entire product will be even.
Method 1: Using Bitwise AND Operator
The bitwise & operation checks the least significant bit (LSB). For any number, num & 1 returns 0 if even, 1 if odd −
def check_product_bitwise(numbers):
for num in numbers:
if not num & 1: # If LSB is 0, number is even
return "Even"
return "Odd"
numbers = [5, 7, 4, 2, 6]
result = check_product_bitwise(numbers)
print("Product of the numbers:", result)
Product of the numbers: Even
Method 2: Calculate Actual Product
This approach computes the actual product and checks if it's divisible by 2 −
numbers = [2, 5, 9, 7, 2]
product = 1
for num in numbers:
product = product * num
if product % 2 == 0:
print("Product of numbers is Even")
else:
print("Product of numbers is Odd")
Product of numbers is Even
Method 3: Check for Even Number Presence
Based on mathematical facts, we only need to check if any even number exists −
def check_product_presence(numbers):
for num in numbers:
if num % 2 == 0:
return "Even"
return "Odd"
numbers = [21, 5, 9, 7, 27]
result = check_product_presence(numbers)
print("Product of numbers:", result)
Product of numbers: Odd
Method 4: Count Even Numbers
If one or more even numbers exist, the product is guaranteed to be even −
def check_product_count(numbers):
even_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
if even_count >= 1:
return "Even"
return "Odd"
numbers = [75, 97, 49, 61, 28]
result = check_product_count(numbers)
print("Product of numbers:", result)
Product of numbers: Even
Performance Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| Bitwise AND | O(n) - stops early | Fast bit operations |
| Actual Product | O(n) | When you need the product value |
| Presence Check | O(n) - stops early | Most readable approach |
| Count Even | O(n) | When you need count information |
Conclusion
The most efficient approach is checking for even number presence, as it stops immediately when an even number is found. Use bitwise operations for performance-critical applications, or the modulo approach for better readability.
---