Even or Odd Python Program | if-else statement
Learn how to use the modulo operator and an
if...else statement to determine
whether a number is even or odd.
By the end of this lesson, you will be able to check whether a number entered by the user is even or odd.
What are even and odd numbers?
An even number can be divided by 2 without a remainder. Examples include 2, 4, 6, 8 and 10.
An odd number leaves a remainder when divided by 2. Examples include 1, 3, 5, 7 and 9.
Use the modulo operator
Python uses % to find the remainder
after division.
10 % 2 == 0
10 divided by 2 leaves a remainder of 0, so 10 is an even number.
Even or odd number program
First, take a number from the user. Then use
% 2 to check its remainder.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
How does the program work?
input() asks the user to
enter a number.
int() converts the entered
value into an integer.
num % 2 == 0 checks whether
the number leaves a remainder of zero
when divided by 2.
If the condition is true, Python prints Even number. Otherwise, it prints Odd number.
Try the program with 25
To check whether a number is even, use
number % 2 == 0. If the result is
true, the number is even; otherwise, it is odd.
Even or Odd Python Program | if-else statement
Even-odd numbers, Python tutorial | If Else statement Python | Even or Odd Number Program | Learn Python | modulo operator tutorial
Related
About this Python course
This Python course introduces beginners to programming through simple lessons, examples and step-by-step explanations.
The lessons gradually introduce Python concepts such as printing output, variables, data types, input, conditions, loops and other programming fundamentals.