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
- Start
- Take input: Prompt the user to enter a number.
- Check the number:
- If the number is divisible by 2 (i.e.,
number % 2 == 0
), it is even. - Otherwise, it is odd.
- If the number is divisible by 2 (i.e.,
- Display the result: Print whether the number is odd or even.
- 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
- Taking input from the user:
Theinput()
function is used to take input from the user. The input is converted to an integer usingint()
because we are working with whole numbers. - Checking if the number is odd or even:
The program checks whether the number is divisible by 2 using the modulus operator (%
). Ifnum % 2 == 0
, the number is even; otherwise, it is odd. - Displaying the output:
Theprint()
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.