An even or odd number program in Python is a simple beginner-level program used to check whether a given number is divisible by 2. If the remainder is 0, the number is even; otherwise, the number is odd. This program helps beginners understand user input, conditional statements, and the modulus (%) operator in Python.
Table of Contents
What is an Even Number?
An even number is a number that is completely divisible by 2 without leaving any remainder.
Examples:
2, 4, 6, 8, 10, 12
For example, 8 ÷ 2 = 4, so 8 is an even number.
What is an Odd Number?
An odd number is a number that is not completely divisible by 2. It leaves a remainder of 1 when divided by 2.
Examples:
1, 3, 5, 7, 9, 11
For example, 7 ÷ 2 leaves a remainder of 1, so 7 is an odd number.
How to Check Even or Odd Numbers in Python?
Python uses the modulus operator % to find the remainder after division.
The basic condition is:
number % 2 == 0
If the condition is True, the number is even. Otherwise, the number is odd.
Using the Modulus Operator
The modulus operator % returns the remainder of a division.
# Python program to check the remainder of a number number = 10 remainder = number % 2 print(remainder)
Output:
0
Since the remainder is 0, 10 is an even number.
Even or Odd Number Program
The following program checks whether a given number is even or odd.
# Python program to check whether a number is even or odd
number = 7
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Output:
The number is odd.
Using an if-else Statement
The if-else statement is used to make the decision.
- if: Python checks whether the remainder of the number divided by 2 is 0.
- Even: If the condition is true, Python displays that the number is even.
- else: If the condition is false, Python executes the else block.
- Odd: The program displays that the number is odd.
Taking a Number from the User
The input() function can be used to take a number from the user.
# Python program to check whether a user-entered number is even or odd
number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Output:
Enter a number: 12
The number is even.
Checking Multiple Numbers
A loop can be used to check whether multiple numbers are even or odd.
# Python program to check multiple numbers as even or odd
numbers = [10, 15, 20, 25, 30]
for number in numbers:
if number % 2 == 0:
print(number, "is even.")
else:
print(number, "is odd.")
Output:
10 is even.
15 is odd.
20 is even.
25 is odd.
30 is even.
Complete Even or Odd Program
The following program takes a number from the user and checks whether it is even or odd.
# Python program to check whether a number is even or odd
try:
number = int(input("Enter an integer: "))
if number % 2 == 0:
print(number, "is an even number.")
else:
print(number, "is an odd number.")
except ValueError:
print("Please enter a valid integer.")
Output:
Enter an integer: 24
24 is an even number.
Advantages
- Easy to Understand: The program uses simple Python concepts that are suitable for beginners.
- Simple Logic: The modulus operator provides a simple way to determine whether a number is divisible by 2.
- Fast Execution: The program requires only a simple arithmetic operation and condition check.
- Useful for Learning: The program helps beginners understand operators and conditional statements.
- Useful in Applications: Even and odd number checking can be used as part of larger mathematical and programming applications.
Common Beginner Mistakes
- Using Division Instead of Modulus: Beginners may use / instead of % when checking the remainder.
- Using the Wrong Condition: The correct condition for an even number is number % 2 == 0.
- Forgetting Integer Conversion: Input received using input() is a string, so int() should be used when an integer is required.
- Incorrect Indentation: Python requires proper indentation inside if and else blocks.
- Forgetting to Handle Invalid Input: The program may produce an error if the user enters non-numeric input, so input validation can be added when required.
Conclusion
Checking whether a number is even or odd is a simple Python program that uses the modulus operator and an if-else statement. If the remainder after dividing a number by 2 is 0, the number is even; otherwise, it is odd. This example is useful for understanding basic operators, conditions, and user input in Python.
Frequently Asked Questions
1. What is an even number?2. What is an odd number?An even number is a number that is completely divisible by 2 without leaving a remainder.
3. Which operator is used to check whether a number is even or odd?An odd number is a number that leaves a remainder of 1 when divided by 2.
4. What condition is used to check an even number in Python?The modulus operator % is used to find the remainder and check whether a number is even or odd.
5.Can Python check multiple numbers for even or odd values?The condition number % 2 == 0 is used to check whether a number is even.
Yes. A for loop or another looping structure can be used to check multiple numbers.
0 Comments