Python program to check if a number is odd or even

In this article, you will learn to write a Python program to check if a number is odd or even. The problem statement is that find whether the given number is Even or Odd Using Python Programming language.

Here you will see the python code to check the even-odd number using the if-else and conditional operator with the help of modulus and bitwise operator.

First Let’s understand what even and odd numbers are. It helps you in writing the code.

An integer number divisible by 2 is called an even number. For example: 12, 18, 30, 16, . . . , etc.

And the integer numbers that are not exactly divisible by 2 are not known as odd numbers. For example :31, 7, 11, 21, . . . , etc.

 

Also, let’s understand the modulus and “bitwise And Operator” it helps you understand the code if you are a beginner.

1.  Modulus Operator:

The result of the % (modulus operator) is the remainder. It returns the remainder of dividing the left operand by the right operand. It would be best if you remembered that the right operand must not be zero.

>>> 15 % 2
1

>>> 20 % 2
0

>>> 6 % 2
0

>>> 13 % 2
1

 

2.  Bitwise And Operator:

The result of the binary & operator is the bitwise AND of the operands. That means each bit in the result is set if and only if each of the corresponding bits in the converted operands is set.

Let’s understand it with an example.

Expression Binary Value Decimal Value
num1 000110112 2710
num2 000000102 210
num1 & num2 000000102 210

 

 

Python programs to check if a number is Odd or Even:

Let’s see some ways to find even or odd numbers using the python programming language.

Method 1: Using the Modulus Operator and if-else condition:

In this example code, you will see how you can use the modulo operator to determine if a number is even or odd with the help of the if-else statement. If the modulus of a number divided by 2 is 0 that means the number is even otherwise odd.

# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 0, it is an even number.

num = int(input("Enter a number: "))
if (num % 2) == 0:
   print(num,"is Even")
else:
   print(num,"is Odd")

Output 1:

Enter a number: 5
5 is Odd

Output 2:

Enter a number: 14
14 is even

 

Method 2: Using the Modulus Operator and conditional operator:

In this example besides using the if-else, I have used the ternary conditional operator.

# Python program to check if a given number 
# is odd or even using the conditional operator

num = int(input("Enter a number: "))

print("Even number" if ((num % 2) == 0) else "Odd number")

 

 

Method 3: Using the Bitwise And Operator if-else statement:

You can use the Bitwise And Operator to check whether a given number is even or odd using the python programming language. You just need to check the LSB of the given integer number with the help of bitwise and operator. If the LSB of the given number is set, then that means the number is odd otherwise even.

Here is an example for better understanding:

Example 1:

Input: 9    // odd
 
   0000000000001001              
 & 
   0000000000000001                
-------------------                
   0000000000000001       
-------------------

 

Example 2:

Input: 10     //even

   0000000000001010              
 & 
   0000000000000001                 
-------------------               
   0000000000000000        
-------------------

 

Consider the below code to check even odd using the bitwise operator.

# Python program to check if a given number is odd or even 
#using bitwise operator and if-else statement

num = int(input("Enter a number: "))


# if num & 1 == 1, then num is odd
if (num & 1):
   print(num,"is Odd")
# if num & 1 == 0, then num is even
else:
   print(num,"is Even")

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *