Statement
Solution
Exercise 2.1: Even or Odd Number
Write a program that takes an integer input from the user and checks whether it is Even or Odd.
Your program should:
- Ask the user to enter a whole number (integer).
- Use the Modulo Operator (%) to check the remainder when divided by 2.
- If the remainder is 0, print "Even".
- Otherwise (else), print "Odd".
Sample Interaction:
Input
Output
7
Enter a number: 7
--------------------
7 is an Odd number.
10
Enter a number: 10
--------------------
10 is an Even number.
Solution
We use the % operator to find the remainder. If a number is divisible by 2 with no remainder, it is even.
# 1. Get User Input
num = int(input("Enter a number: "))
print("-" * 20)
# 2. Check Condition
if num % 2 == 0:
print(f"{num} is an Even number.")
else:
print(f"{num} is an Odd number.")
Key Concepts:
%(Modulo) returns the remainder of division.5 % 2is1.4 % 2is0.if condition:executes code only if the condition is True.else:executes code if the initial condition was False.
Test Console
Run code to see output...