Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check if a Number is Even or Odd

Use a remainder to classify whole numbers as even or odd. This first Python condition exercise includes zero, negative integers, and the meaning of modulo.

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

In this one

  • Recognize integer parity
  • Calculate a remainder
  • Include zero and negatives

How it works

  1. 1

    Convert the response to int.

  2. 2

    Calculate num % 2.

  3. 3

    Print Even for zero remainder, otherwise Odd.

Edge cases

Is zero even?

Is zero even?

Yes. Zero divided by two has remainder zero.

Does -3 work?

Does -3 work?

Yes. In Python, -3 % 2 is 1, so the result is Odd.

What's going on

Parity means whether a whole number divides evenly by two. int converts the input text, and num % 2 computes the remainder, not the quotient.

A remainder of zero selects Even; otherwise the program selects Odd. For 8, the remainder is zero. For 7, it is one. The code needs only two branches because every integer belongs to exactly one group.

Is zero even? Yes. Zero divided by two has remainder zero.

Does -3 work? Yes. In Python, -3 % 2 is 1, so the result is Odd.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which operator tests the remainder?

Pick an answer to check it.

Question 2

Is zero even?

Pick an answer to check it.

Questions people ask

Is zero even?

Yes. Zero divided by two has remainder zero.

Does -3 work?

Yes. In Python, -3 % 2 is 1, so the result is Odd.

Which operator tests the remainder?

%.