In this article, we will learn about python program to check if a number is Odd or Even. We will check it in many ways in python.
Getting Started
The task is to find if a given number is odd or even using python program. For example,
If a = 10, then, output should be “Even”. If a = 11, output should be “Odd”.
We can achieve above task in python in many ways –
- Using Division
- Using Bitwise Operator
- Using built-in divmod Function
- Using Lambda Function
- Using Bitwise Right Shift
Using Division
We can check if a number is odd or even by using division operation.
When a number is divided by 2 –
- if remainder is 0, number is even.
- if remainder is 1, number is odd.
Using above technique, we can write python program to check if number is odd or even as shown below –
def is_even_or_odd(number): return "Even" if number % 2 == 0 else "Odd" n = int(input("Enter a number")) result = is_even_or_odd(n) print(f"{n} is {result}")
Output
Enter a number 124 124 is Even
Using Bitwise Operator
We can also use bitwise AND operation with given number to find out if that number is odd or even.
When we convert any number in bits, we get series of 1’s and 0’s. Check out below table to understand bitwise conversion of some numbers
Number | Bitwise Conversion | End Digit |
0 | 0 | 0 |
1 | 1 | 1 |
2 | 10 | 0 |
3 | 11 | 1 |
4 | 100 | 0 |
5 | 101 | 1 |
6 | 110 | 0 |
7 | 111 | 1 |
8 | 1000 | 0 |
9 | 1101 | 1 |
10 | 1010 | 0 |
Notice that all the odd numbers have 1 as unit or end digit when converted in binary form. Similarly, all even numbers have 0 as unit or end digit.
That’s why when we do bitwise AND operation of any number with 1, it gives us 1 as result if number is odd, else it gives us 0.
Now, let’s see how we can use above technique and write python program to check if number is odd or even –
def is_even_or_odd(number): return "Even" if number & 1 == 0 else "Odd" n = int(input("Enter a number")) result = is_even_or_odd(n) print(f"{n} is {result}")
Output
Enter a number 23 23 is Odd
Using built-in divmod Function
divmod function returns a tuple that contains quotient and remainder when first argument is divided by second argument.
Using divmod function, we can write a python program to check if number is odd or even as shown below –
def is_even_or_odd(number): _, remainder = divmod(number, 2) return "Even" if remainder == 0 else "Odd" n = int(input("Enter a number")) result = is_even_or_odd(n) print(f"{n} is {result}")
Output
Enter a number 54 54 is Even
Here,
- divmod function return quotient and remainder when number is divided by 2.
- After that, we check if remainder is 0 or not.
- If remainder is 0, number is even, else it’s odd.
Using Lambda Function
We can write lambda function that will return Odd or Even depending upon number is odd or even.
is_even_or_odd = lambda number: "Even" if number % 2 == 0 else "Odd" n = int(input("Enter a number")) result = is_even_or_odd(n) print(f"{n} is {result}")
Output
Enter a number 789 789 is Odd
Using Recursion
This is just an example of how you can write python program to check if number is odd or even using recursion. However, it is advisable that you do not use it. There are many better ways to check that and we have already discussed about it earlier in this post.
def is_even_or_odd(number): if number == 0: return "Even" elif number == 1: return "Odd" else: return is_even_or_odd(number - 2) n = int(input("Enter a number")) result = is_even_or_odd(n) print(f"{n} is {result}")
Output
Enter a number 321 321 is Odd
Using Bitwise Right Shift
Check how we can use bitwise right and left shift operation to check if number is odd or even as shown below –
def is_even_or_odd(number): return "Even" if (number >> 1) << 1 == number else "Odd" n = int(input("Enter a number")) result = is_even_or_odd(n) print(f"{n} is {result}")
Here,
- (number >> 1) shifts number bitwise to right by 1 position. This is same as dividing the number by 2 and discarding the remainder.
- << 1 shifts number bitwise to left by 1 position. This is same as multiplying the number by 2.
- So, (number >> 1) << 1 checks if number is divisible by 2 or not. If number is even, we will get result of the expression same as the original number. But, if number is odd, we will get result of the expression less than the original number by 1.
Output
Enter a number 54 54 is Even
Thus, we have seen how to write a python program to check if number is odd or even using
– Recursion,
– Bitwise right and left shift,
– Bbitwise AND operation, or
– Division operation
– divmod built-in function
Checkout exclusive collection of different programming examples of python.
If you want to know more about python, visit official documentation.