Check if a Number is Even or Odd

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

Logic BuildingBeginner
Logic Building
# Take a number as input
num = int(input("Enter a number: "))

# Check if even or odd
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

Output

Enter a number: 8
Even

Enter a number: 7
Odd

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, ...