Identify whether the number is Even or Odd in Python

If any number is exactly divisible by 2 then it’s an even number else it’s an odd number.

In this tutorial we’ll see how to check whether the provided number is even or odd number using Modulo(%).

Flowchart

Flowchart for odd even
Flowchart for odd even

Note

  • The remainder calculated with %(Modulo) on dividing by 2, will always be 0 for all even numbers.
  • Whereas the remainder on dividing by 2, will always be 1 for all odd numbers.

Code

a = int(input('Enter number: '))
rem = a % 2

print('A:', a)
print('Remainder:', rem)

if rem == 0
    print(a, 'is an even number')
else:
    print(a, 'is an odd number')

'''
    Remainder after dividing by 2
    12 -> 0
    13 -> 1
    14 -> 0
    15 -> 1
    16 -> 0
    17 -> 1
    18 -> 0
    19 -> 1
    20 -> 0
    21 -> 1
    22 -> 0
'''

Output

Enter number: 58147
A: 58147
Remainder: 1
58147 is an odd number

Happy 😄 coding

Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Previous

Related