This Python example code demonstrates a simple Python program that checks whether a given number is an even or odd number and prints the output to the screen.
Program:
#Taking user input
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Program Output:
Enter a number: 6 6 is Even
If a number is evenly divisible by 2 without any remainder, then it is an even number; Otherwise, it is an odd number. The modulo operator %
is used to check it in such a way as num%2 == 0
.
In the calculation part of the program, the given number is evenly divisible by 2 without remainder, so it is an even number.