Python Program to check whether a number is even or odd

Write a Python program that checks whether a given number is odd or even.

Input-Output Examples

plaintext
Example 1:
Input: Enter a number: 4
Output: The number is even.

Example 2:
Input: Enter a number: 7
Output: The number is odd.

Algorithm to check if a number is even or odd 

  1. Start
  2. Take input: Prompt the user to enter a number.
  3. Check the number:
    • If the number is divisible by 2 (i.e., number % 2 == 0), it is even.
    • Otherwise, it is odd.
  4. Display the result: Print whether the number is odd or even.
  5. End

Python Program to check whether a given number is even or odd

python
# Python program to check if a number is odd or even

# Take input from the user
num = int(input("Enter a number: "))

# Check if the number is divisible by 2
if num % 2 == 0:
    print(f"The number {num} is even.")
else:
    print(f"The number {num} is odd.")

Code Explanation

  1. Taking input from the user:
    The input() function is used to take input from the user. The input is converted to an integer using int() because we are working with whole numbers.
  2. Checking if the number is odd or even:
    The program checks whether the number is divisible by 2 using the modulus operator (%). If num % 2 == 0, the number is even; otherwise, it is odd.
  3. Displaying the output:
    The print() function is used to display whether the number is odd or even. An f-string is used to format the output clearly by including the input number in the result.

Python

7715

123

Related Articles