← Go Back
The following console program requests an integer and indicates whether it is even or odd:
How to Determine if a Number Is Even or Odd
Using the remainder operator (%
). For any integer n
, if n % 2
returns zero, n
is even; if it returns one, it is odd.>>> a = 10
>>> b = 11
>>> a % 2
0
>>> b % 2
1
The following console program requests an integer and indicates whether it is even or odd:
n = int(input("Enter a number: "))
if n % 2 == 0:
print(n, "is even.")
else:
print(n, "is odd.")
🐍 You might also find interesting: