Python Program to check a number is Even or Odd
Summary:
Problem Statement: Write a Python Program to check if a number is Even or Odd.
Description: Even numbers are those numbers which are completely divisible by 2 and Odd numbers are those numbers which are not completely divisible by 2.
Example:
Input: 6
Output: It is Even Number.
Explanation: 6 is completely divisible by 2 because 6%2=0 (remainder).
Input: 7
Output: It is Odd Number.
Explanation: 7 is not divisible by 2 because 7%2=1 (remainder).
Hint: The logic to check if a number is even is num%2==0
. Now you have the logic, it is easy to write the Python program to check if a number is Even or odd.
Submit your solution in the comments.
Python Program: Check a number is Even or Odd
Copy
num = int(input("Enter the Number: "))
if(num%2==0):
print("{} is Even Number".format(num))
else:
print("{} is Odd Number".format(num))
Output
Enter the Number: 6 6 is Even Number
The main logic of this Python code lies in the second line.
We check the remainder is 0 when the number is divided by 2. If it is true then it is Even number else it is an odd number.
One-Line Python Logic (Advanced)
It is possible to write the above logic in one line to check if a number is even or odd.
Copy
num = int(input("Enter the Number: "))
print("It is Even Number." if(int(input("Enter the Number: "))%2==0) else "It is Odd Number." )
Output
Enter the Number: 3 It is Odd Number.
We can also write the if-else condition in one line.
The logic is similar to the previous Program, we only change the if-else statement and take the input()
inside if statement.
Related Python Examples:
Join Our Youtube Channel
Subscribe