In: Computer Science
using python:
Given an arbitrary whole number, please write an if statement to output if it is an even number or an odd number
We know that a number is even if it is exactly divisible by 2, i.e., remainder is 0 . So we make use % operator available in python to check whether the remaninder is 0 when divided by 2. If the remainder is 0 we print that the number is an even number, else we print that the number is an odd number.
COde for the above operation is given below.
num=int(input("Enter the number")); #taking input from the user
if (num%2==0): #checking for the remaninder
print(num,'is an even number'); # if 0 print that it is even number
else:
print(num,'is an odd number'); # else print it is odd number
output for variour inputs are shown below.
Hope you got the answer. If you still have any doubts regarding this, please try to comment. I will help you with that.