Given number is even or odd in Python

Write a Program to given number is even or odd

Python Examples-InterviewGIGHere’s a program written in Python to determine whether a given number is even or odd, along with an explanation and the expected output:

# Taking input from the user
num = int(input("Enter a number: "))
if num % 2 == 0: # Checking if the number is even or odd
    print("Even")
else:
    print("Odd")

Explanation:

  • The program starts by prompting the user to enter a number using the input() function. The int() function is used to convert the input into an integer.
  • The number is stored in the variable num.
  • The program uses an if statement to check whether the remainder of dividing num by 2 is equal to 0. If the remainder is 0, the number is even; otherwise, it’s odd.
  • If the condition in the if statement is satisfied (i.e., the number is even), the program prints “Even”. Otherwise, it prints “Odd”.

Output (Odd) 1:

Enter a number: 7
Odd

In this example, if the user enters the number 7, the program checks whether 7 % 2 equals 0. Since it’s not equal to 0, the program determines that 7 is an odd number and displays “Odd” as the output.

Output (Even) 2:

Enter a number: 8
even

In this example, if the user enters the number 8, the program checks whether 8 % 2 equals 0. Since it’s equal to 0, the program determines that 8 is an even number and displays “even” as the output.

Scroll to Top