How to Check if a Number is Odd or Even using Python?



In this article, we will show you how to check if a number is odd or even in python. Below are the methods to accomplish this task −

  • Using modulo (%) operator
  • Using Recursion
  • Using the Binary AND (&) operator

Using modulo (%) operator

Python's modulo (%) operator (also called the remainder operator) is useful to determine if a number is odd or even. We obtain the remainder of the division of a number by 2. If it is 0, it is even otherwise it is odd

Even Number − A number that can be divided by 2, leaving no remainders(remainder=0).

Odd Number − An odd number is one that cannot be divided by 2, so the remainder is 1.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input number.

  • Use the if conditional statement to check whether the input number modulus 2 is equal to 0 using the modulus (%)operator(returns the remainder).

  • Print even if the condition is true i.e, the remainder is 0.

  • Else Print odd if the condition is false i.e, the remainder is not 0.

Example

The following program returns the whether the input number is an even or an odd number using the modulo (%) operator −

# input number inputNumber = 10 # checking whether the number modulus 2 is equal to 0 if inputNumber%2==0: # printing even if the remainder is 0 print(inputNumber, "is an even number") else: # else printing odd print(inputNumber, "is an odd number")

Output

On executing, the above program will generate the following output −

10 is an even number

Using Recursion

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a function checkEvenOdd to check whether the number passed to it as an argument is an even or odd number.

  • Use the if conditional statement to check whether the number is 0 and if it is 0 then the given number is even so return True.

  • Use another if conditional statement to check whether the number is 1 and if it is 1 then the given number is odd so return False.

  • Call the function again recursively by subtracting 2 from the given number.

  • Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.

  • Print even if the function returns True.

  • Else print odd if the function returns False

Example

The following program returns the whether the input number is an even or an odd number using the recursive function −

# creating a function that accepts a number as an argument and # checks whether it is an odd or even number def checkEvenOdd(num): # checking whether the number is 0 if(num==0): # returning True, if the number is even return True # checking whether the number is 1 elif(num==1): # returning False, if the number is odd return False else: # Calling the function again recursively by subtracting 2 from the given number return checkEvenOdd(num-2) # input number inputNumber= 7 # passing inuput number as an argument to the checkEvenOdd() and calling it if(checkEvenOdd(inputNumber)): # printing even if the function returns true print(inputNumber, "is an even number") else: # else printing odd if the function returns false print(inputNumber, "is an odd number")

Output

On executing, the above program will generate the following output −

7 is an odd number

Using Binary AND (&) operator

The idea is to check if the last bit of the number is set. If the last bit is set, the number is odd, otherwise it is even.

As you can see, bitwise ANDing(&) a number through a 1 gives a 1 if the number is odd because the last bit is already set. Otherwise, 0 is returned as output.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the if conditional statement to check whether the binary and(&) operation between the number passed and 1 is equal to 0 using the & operator.

  • If the condition is true, then the number is even and returns True.

  • Else the given number is odd so return False.

  • Create a variable to store the input number.

  • Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.

  • Print even if the function returns True.

  • Else print odd if the function returns False.

Example

The following program returns the whether the input number is an even or an odd number using the binary AND (&) operator −

# creating a function that accepts a number as an argument and # checks whether it is an odd or even number def checkEvenOdd(num): # checking whether num&1 == 0 if num & 1 == 0: # Then the number is even so return True return True # Else the number is odd # Then the number is odd so return False return False # input number inputNumber= 12 # passing input number as an argument to the checkEvenOdd() and calling it if(checkEvenOdd(inputNumber)): # printing even if the function returns true print(inputNumber, "is an even number") # else printing odd if the function returns false print(inputNumber, "is an odd number")

Output

On executing, the above program will generate the following output −

12 is an even number
12 is an odd number

Conclusion

In this article, we learned how to use three different methods to determine whether a given number is even or odd. We learned how to check the last bit of a given number using bitwise operators. We learned how to recursively call the function.


Advertisements