Solution :
num = int(input(“Enter the number: “)) # Taking input from the user and converting it to an integer
if num % 2 == 0: # Checking if the number is divisible by 2 (i.e., even)
print(f”The given number is even: {num}”) # Printing that the number is even
else: # If the condition above is false, the number is odd
print(f”The given number is odd: {num}”) # Printing that the number is odd
Line-by-Line Explanation:
num = int(input("Enter the number: "))
- The program takes input from the user using
input()
. input()
always takes input as a string, so we useint()
to convert it to an integer.
- The program takes input from the user using
if num % 2 == 0:
%
is the modulus operator, which returns the remainder whennum
is divided by2
.- If
num % 2 == 0
, it means the number is even because it is perfectly divisible by2
.
print(f"The given number is even: {num}")
- If the condition
num % 2 == 0
is true, this statement is executed. - The
f""
(formatted string) allows us to directly insert the value ofnum
inside the string.
- If the condition
else:
- If the
if
condition is false (i.e.,num % 2
is not0
), then the number is odd.
- If the
print(f"The given number is odd: {num}")
- If the number is not even, this statement is executed, indicating that the number is odd.