Learn.Dedaandsons
Learn.Dedaandsons Python for Beginners
💻 if else Python

Check if a Number Is Even or Odd in Python | if-else statement | modulo operator tutorial.

Course progress Lesson if else Python
Lesson 4 / 5

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.

🎯
What you'll learn

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.

Python
10 % 2 == 0
Result True

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.

Python

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even number")
else:
    print("Odd number")

How does the program work?

1
Get the user's number

input() asks the user to enter a number.

2
Convert the input

int() converts the entered value into an integer.

3
Check the remainder

num % 2 == 0 checks whether the number leaves a remainder of zero when divided by 2.

4
Choose the result

If the condition is true, Python prints Even number. Otherwise, it prints Odd number.

Try the program with 25

Input 25
Check 25 % 2 remainder = 1
Output Odd number
Python program checking whether a number is even or odd
💡 Key takeaway

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