In this Python lesson, we will practice how to write a program to check if a number is odd or even using condition/decision making statement.
We can say that a number is even if it is divisible by 2 without a remainder. Using the modulus operator %, if the remainder is not zero when divided by 2, the number is odd.
Example: Check Even or Odd Number using if-else Condition
# request input from user
num = int(input('Enter any number: '))
if (num % 2) == 0:
print('{0} is an Even Number'.format(num))
else:
print('{0} is an Odd Number'.format(num))
Output
Enter any number: 7
7 is an Odd Number
Here, we created a program that requested a numeric input from the user and checks if it is an Even or Odd number using the conditional if-else statement. You can test for an even number by inserting a number when user is prompted to “Enter any number”.