1 ) Write a program that checks whether the given input is an even number or an  odd number ?

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:

  1. num = int(input("Enter the number: "))
    • The program takes input from the user using input().
    • input() always takes input as a string, so we use int() to convert it to an integer.
  2. if num % 2 == 0:
    • % is the modulus operator, which returns the remainder when num is divided by 2.
    • If num % 2 == 0, it means the number is even because it is perfectly divisible by 2.
  3. 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 of num inside the string.
  4. else:
    • If the if condition is false (i.e., num % 2 is not 0), then the number is odd.
  5. print(f"The given number is odd: {num}")
    • If the number is not even, this statement is executed, indicating that the number is odd.

Scroll to Top