In this tutorial, we are going to learn a python program to find a number given as input from the user is even or odd.
Problem Statement
For any number that is input by the user, we have to check if it is even or odd.
For example:
Case1: If the user inputs number 51
then the output should be ‘odd’.
Case2: If the user inputs a number 100.
then the output should be ‘even’.
How to check if a number is an even number?
An integer divided by ‘2’ and leaves the remainder 0 is called an even number. We can also define an even number as a number that ends with either 0 or 2 or 4 or 6 or 8 is considered an even number.
All the numbers other than even number is considered odd number. Odd numbers always leave the remainder ‘1’ when divided by ‘2’.
Our program divides the number by ‘2’. If the remainder is ‘0’, we consider a number an even number; otherwise, it is an odd number.
Our Logic to check given number is even or odd
- Our program will take a number as input from the user.
- The input number is checked if it is an even number or not.
- If it is, then print ‘even,’ and if it is not, the result should print ‘odd.’
Algorithm to check given number is even or odd
Step 1: Start
Step 2: take input from the user.
Step 3: if number is divisible by 2:
Print ‘even’
else:
Print ‘odd’
Step 4: Stop
Python Code to check given number is even or odd
Program 1: Basic Modulus Operator
#taking input from the user
num = int(input("Enter a number to check even/odd "))
#if number is divisible by 2
if num%2 == 0:
print(num,"is even number")
else:
print(num,"is odd number")
Output:
Enter a number to check even/odd 5 5 is odd number
Explanation:
The input integer is 5, which is then checked if it’s divisible by 2.
5%2 = 1, which is not divisible by 2, this makes the integer 5 an odd integer
Program 2: Using Bitwise Operator
This method utilizes the bitwise AND operator to check the least significant bit (LSB) of the number.
number = int(input("Enter a number to check if it is even or odd: "))
result = "Even" if (number & 1) == 0 else "Odd"
print(f"The number {number} is {result}.")
Output:
Enter a number to check if it is even or odd: 5 The number 5 is Odd.
Explanation:
In binary, even numbers always end in 0, and odd numbers in 1. The expression (num & 1)
evaluates to 1 if the number is odd and 0 if it is even, based on the LSB.
Program 3: Using Lambda Function
This approach shows how to use a lambda function for a more concise code snippet to accomplish the task.
check_even_odd = lambda num: "Even" if num % 2 == 0 else "Odd"
number = int(input("Enter a number to check even/odd: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")
Output
Enter a number to check even/odd: 6 The number 6 is Even.
Explanation:
This single-line function (lambda) returns “Even” if the number is divisible by 2 without a remainder; otherwise, it returns “Odd”. It’s a compact version of the first method.
Program 4: Using Integer Division
This method involves checking the twice divided value of the number.
def check_even_odd(num):
return "Even" if (num // 2) * 2 == num else "Odd"
number = int(input("Enter a number to check if it is even or odd: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")
Output
Enter a number to check even/odd: 6 The number 6 is Even.
Explanation
This function checks if a number, when halved and then doubled, returns to the original number. This happens with even numbers but not with odd ones.