In this post, I am going make a Python’s program which will check whether a number is even or odd. But before making program, first you need to understand modulus operator %.
num=4
r=num%2
print(r)
The above code defined a variable num which refers to 4, and variable r refers remainder when 2 divides num, so r will refer 0.
num=5
r=num%2
print(r)
The above code defined a variable num which refers to 5, and variable r refers remainder when 2 divides num, so r will refer 1.
Both concepts we will use to check whether a program is odd or even.
num=int(input("Enter a Number")
if num%2==0:
print("Number is Even")
else:
print("Number is Odd)"
print(r)
Consider the above program, if you take 4 as input num%2==0 will result as True and output would be “Number is Even”. If you take 5 as input num%2==0 will result as False and control will get transferred to else and output would be “Number is Odd”.