Beginner Number Problems Python

Even or Odd Program in Python with Output

Check whether a given integer is even or odd using the modulus operator and an if-else statement.

  • TimeO(1)
  • SpaceO(1)
  • Hints3
  • Read3 min
Quick answer

A number is even if it leaves no remainder when divided by 2, and odd if the remainder is 1. In Python, n % 2 gives that remainder, so the check is: if n % 2 == 0: print("even") else: print("odd"). It works for 0 and for negative numbers too, because in Python -7 % 2 is 1.

Modulus operatorif-elseinput() and int()for loop with range()
1

Problem statement

Write an even or odd program in Python. Take an integer and print whether it is even or odd.

Use an if-else statement and the modulus operator %. Test it with 10, 7, 0 and a negative number.

Input

A single integer n.

Output

"<n> is an even number" or "<n> is an odd number".

Examples

Example 1
Inputn = 10
Output10 is an even number

10 % 2 is 0, so 10 is even.

Example 2
Inputn = 7
Output7 is an odd number

7 % 2 is 1, so 7 is odd.

Example 3
Inputn = 0
Output0 is an even number

0 % 2 is 0. Zero is even.

Constraints

  • n can be any integer, positive, negative or zero
  • Python integers have no fixed size, so very large values also work
2

Think before you code

Reveal the questions to ask yourself first
  • What is left over when you divide an even number by 2? And an odd number?
  • Which Python operator gives you only the remainder?
  • Do you need two if checks, or is one if with an else enough?
3

Hints

Open one at a time — try again after each.
1 Hint 1
n % 2 gives the remainder after dividing n by 2. It is always 0 or 1 in Python.
2 Hint 2
Compare with ==, not =. A single = is assignment and is a syntax error inside if.
3 Hint 3
input() returns a string. Wrap it in int() before using %.
4

Approach

Reveal the step-by-step approach

Check even odd in Python with if-else

  1. Read the number and convert it to int.
  2. If n % 2 == 0, print that it is even.
  3. Otherwise (else), print that it is odd.

There are only two possible remainders when you divide by 2, so one if and one else cover every case. You do not need elif.

A short note on the modulus operator in Python

% returns the remainder of a division. // returns the whole-number quotient.

Expression Result Why
17 % 2 1 17 = 2 x 8 + 1
18 % 2 0 18 = 2 x 9 + 0
-7 % 2 1 Python keeps the sign of the divisor (2), so the result is 0 or 1
17 // 2 8 Quotient only

In C and Java, -7 % 2 is -1, so a check like n % 2 == 1 fails for negative odd numbers there. Checking n % 2 == 0 is safe in every language.

5

Dry run

Walk through the example step by step

Running the check for a few values:

n  | n % 2 | n % 2 == 0 | branch | output
---+-------+------------+--------+-------
10 | 0     | True       | if     | even
7  | 1     | False      | else   | odd
0  | 0     | True       | if     | even
-7 | 1     | False      | else   | odd
6

Solution

Reveal the full Python solution
solution.py
n = int(input("Enter a number: "))
if n % 2 == 0:
    print(n, "is an even number")
else:
    print(n, "is an odd number")

Output (two separate runs):

Enter a number: 15
15 is an odd number
Enter a number: 42
42 is an even number

int(input(...)) turns the typed text into a number. The if line checks the remainder, and exactly one of the two print lines runs.

Even or odd with a function

Putting the check in a function lets you test many values at once.

def check_even_odd(n):
    if n % 2 == 0:
        return "even"
    else:
        return "odd"


for n in [10, 7, 0, -4, -7]:
    print(n, "is", check_even_odd(n))
10 is even
7 is odd
0 is even
-4 is even
-7 is odd

Even and odd numbers in a range

A common next question is "print the even and odd numbers from 1 to 10".

evens = []
odds = []
for n in range(1, 11):
    if n % 2 == 0:
        evens.append(n)
    else:
        odds.append(n)
print("Even:", evens)
print("Odd:", odds)
Even: [2, 4, 6, 8, 10]
Odd: [1, 3, 5, 7, 9]

One-line version and the bitwise check

n = 25
print("even" if n % 2 == 0 else "odd")
print("even" if n & 1 == 0 else "odd")
odd
odd

The first line is a conditional expression, a one-line if-else. The second looks at the last bit of the number: it is 0 for even numbers and 1 for odd ones. Interviewers sometimes ask for this "without %" version. The Java page check whether a number is even or odd without the modulus operator explains it with a dry run.

For more beginner Python programs, see the leap year program in Python and the palindrome program in Python.

TimeO(1)
SpaceO(1)
7

Review before the interview

Common mistakes

  • Writing if n % 2 = 0. A single = is assignment, and Python reports a SyntaxError.
  • Forgetting int() around input(). "15" % 2 is string formatting, not a remainder, and raises a TypeError.
  • Checking n % 2 == 1 for odd and porting it to C or Java, where negative odd numbers give -1.
  • Using two separate if statements instead of if-else, which is longer and easy to get wrong.

Edge cases to test

  • 0 is even, because 0 % 2 is 0.
  • Negative numbers: -4 is even and -7 is odd. Python gives -7 % 2 == 1.
  • Entering a decimal like 4.5 makes int() raise a ValueError. Even and odd apply only to whole numbers.

Interview follow-ups

  • How would you count the even and odd digits of a number?
  • How would you check even or odd without using the % operator?
  • How would you print only the even numbers from a list using a list comprehension?

Keep practising

Practising Python for interviews?

CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Chat with us