Check whether product of \\\\\'n\\\\\' numbers is even or odd in Python



In this article, we try to find different methods to solve a problem 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, the product is 168 is an even number. 9 and 5 are two odd numbers, the product is 45 is an odd number. Consider one even number 2 and one odd number 3, product of these two numbers is 6 an even number.

Facts to Check if a Product is Even or Odd

Let's figure out the conditions to check if the product is even or odd based on the mathematical calculation facts mentioned above ?

  • The product of two even numbers is an even number
  • The product of two odd numbers is an odd number
  • The product of one even number and one odd number is an even number

Using the 'and' operator

The bitwise & (AND) operation is performed between 1 and each array element. This operation checks the least significant bit (LSB) of each number in the array. If the result of nums[i] & 1 is 0, the number is even (since even numbers have their LSB as 0 in binary). If the result is 1, the number is odd (since odd numbers have their LSB as 1 in binary).

Example

In the following example, the function stops and returns Even as soon as it finds the first even number in the array. If the loop completes without finding any even number, it concludes that all numbers are odd and returns Odd ?

def solve(nums):
   for i in range(len(nums)):
      if not nums[i] & 1:
         return "Even"
 
   return "Odd"
   
nums = [5,7,4,2,6]
print("Product of the n numbers:",solve(nums))

Following is the output of the above code ?

Product of the n numbers: Even

Product of all elements in the array

We can iterate through the array of numbers and calculate the product of all elements in the array. If the product is an even number, it will return Even; otherwise, it returns Odd.

Example

The following code calculates the product of all elements in my_Array using a loop and checks if the product is even or odd by determining if it is divisible by 2, printing the result accordingly ?

my_Array = [2, 5, 9, 7, 2]
mul = 1
for i in my_Array:
    mul=mul*i
   
if mul%2==0:
    print("Product of n numbers is Even")
else:
    print("Product of n numbers is Odd")

Following is the output of the above code ?

Product of n numbers is Even

Checking the Presence of Even Number

Without performing a multiplication based on the mathematical fact we check whether the product of n number is even or odd. If the array considers one even number, the product will be an even number. Otherwise, an odd number.

Example

In the following example, we have determined whether the product of the array elements is even or odd by checking for the presence of at least one even number, as an even number guarantees an even product ?

def Product_even_or_odd(num):
   for i in num:
      if i%2==0:
         return "Even"
   return "Odd"
   
my_Array = [21, 5, 9, 7, 27]
result = Product_even_or_odd(my_Array)
print("Product of n number even or odd:",result)
Product of n number even or odd: Odd

Counting No.of even number

If the set of numbers contains 1 or more than 1 even number it guarantees, that the product of numbers will be an even number.

Example

In the following example, we have counted the number of even numbers in the array. If the count of even numbers is greater than or equal to 1, the product of all the numbers is an even number; otherwise, the product is an odd number ?

def Product_even_or_odd(num):
   count=0
   for i in num:
      if i%2==0:
         count=count+1
   if count>=1:
       return "Even"
   return "Odd"  
my_Array = [75, 97, 49, 61, 28]
result = Product_even_or_odd(my_Array)
print("Product of n numbers even or odd:",result)

Following is the output of the above code ?

Product of n numbers even or odd: Even
Updated on: 2024-12-17T15:57:36+05:30

545 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements