Check if a Number is Even or Odd

Check if a number is even or odd using modulo operator.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 2 of 10
check-if-a-number-is-even-or-odd.py
1# Take a number as input
2num = int(input("Enter a number: "))
3
4# Check if even or odd
5if num % 2 == 0:
6 print("Even")
7else:
8 print("Odd")

Output

Enter a number: 8
Even

Enter a number: 7
Odd

What's going on

The modulo operator (%) returns the remainder after division.

Key Concepts:

num % 2 gives remainder when num is divided by 2
If remainder is 0, number is even
If remainder is 1, number is odd

Logic:

Even numbers: 0, 2, 4, 6, 8, 10, ...
Odd numbers: 1, 3, 5, 7, 9, 11, ...