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
Note
- The remainder calculated with
%(Modulo) on dividing by 2, will always be0for all even numbers. - Whereas the remainder on dividing by 2, will always be
1for 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 % 2 -> 0
13 % 2 -> 1
14 % 2 -> 0
15 % 2 -> 1
16 % 2 -> 0
17 % 2 -> 1
18 % 2 -> 0
19 % 2 -> 1
20 % 2 -> 0
21 % 2 -> 1
22 % 2 -> 0
'''
Output
Enter number: 58147
A: 58147
Remainder: 1
58147 is an odd number
Happy ๐ coding
Related Articles
Deepen your understanding with these curated continuations.
How to Display Multiplication Tables in Python
Learn to generate multiplication tables in Python using for loops and the range() function. A perfect exercise for beginners to master iterative logic.
How to Generate Random Numbers in Python: A Quick Guide
Learn how to generate random integers and floats in Python using the random library. Master randint() and random() functions with simple code examples.
How to Check if a Variable is an Integer in Python
Learn how to check if a variable is an integer in Python using the type() and isinstance() functions. Essential for data validation and type-safe coding.