In this program, we will learn how to check if a number is even or odd using Python Programming language.
If a number is perfectly divisible by 2, then it is an even number. For example: 2, 4, 16, . . . and so on.
In the same way, if a number is not perfectly divisible by 2, then it is an odd number. For example: 3, 5, 7, . . . and so on.
This program asks the user to enter a number, then it checks whether it’s even or odd using % operator to find the remainder.
So, without further ado, let’s begin this tutorial.
Python Program to Check If a Number is Even or Odd
# Python Program to Check If a Number is Even or Odd num = int(input("Enter a number: ")) if (num % 2 == 0): print("{0} is an even number." .format(num)) else: print("{0} is an odd number." .format(num))
Output 1
Enter a number: 7
7 is an odd number.
Output 2
Enter a number: 48
48 is an even number.
How Does This Program Work ?
num = int(input("Enter a number: "))
The user is asked to enter a number. The value of the entered number gets stored in the num named variable.
if (num % 2 == 0): print("{0} is an even number." .format(num)) else: print("{0} is an odd number." .format(num))
Then, we check whether the entered number is divisible by 2 or not. If yes, then the entered number is an ever number otherwise it’s an odd number.
Python Program to Check Whether a Number is Even or Odd
# Python Program to Check If a Number is Even or Odd num = int(input("Please enter a number: ")) # Finding the remainder rem = num % 2 # Checking Even/Odd if rem == 0: print(num, "is an even number.") elif rem == 1: print(num, "is an odd number.") else: print("Error!!")
Output
Please enter a number: 72
72 is an even number.
Conclusion
I hope after going through this post, you understand how to check if a number is even or odd using Python Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.
Also Read: