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.
- Time
O(1) - Space
O(1) - Hints3
- Read3 min
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.
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.
A single integer n.
"<n> is an even number" or "<n> is an odd number".
Examples
n = 1010 is an even number10 % 2 is 0, so 10 is even.
n = 77 is an odd number7 % 2 is 1, so 7 is odd.
n = 00 is an even number0 % 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
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
ifchecks, or is oneifwith anelseenough?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Check even odd in Python with if-else
- Read the number and convert it to
int. - If
n % 2 == 0, print that it is even. - 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.
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
Solution
Reveal the full Python solution
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.
O(1)O(1)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
Check Whether a Number Is Even or Odd Without the Modulus Operator
Hints · Dry run · SolutionSolve Similar problemCount the Even and Odd Numbers in an Array
Hints · Dry run · SolutionSolve Similar problemCount the Even and Odd Digits in a Number
Hints · Dry run · SolutionSolve Similar problemLeap Year Program in Python with Output
Hints · Dry run · SolutionSolve Related learningWhat Is Python Used For
TutorialLearn Related learningIf Else Statements
TutorialLearn Related learningPython Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising Python for interviews?
CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
