Python Program to Check Whether a Number is Even or Odd
Determining whether a number is even or odd is one of the most fundamental programming tasks. It helps beginners understand conditional statements, arithmetic operations, and logical thinking in Python. In this tutorial, we will explore how to check whether a number is even or odd using Python, along with different approaches, examples, and real-world applications.
What is an Even or Odd Number?
An even number is any integer that is divisible by 2 without leaving a remainder. Examples include 2, 4, 6, 10, and so on. An odd number, on the other hand, is not divisible by 2 and always leaves a remainder of 1 when divided by 2. Examples include 1, 3, 5, 7, and so on.
Why Learn This Program?
This program introduces key programming concepts such as conditional statements (if-else), operators, and user input handling. It is also widely used in algorithms, data processing, and real-world applications such as validation systems and filtering logic.
Logic Behind Even or Odd
To determine whether a number is even or odd, we use the modulo operator (%). The modulo operator returns the remainder of a division. If a number divided by 2 leaves a remainder of 0, it is even; otherwise, it is odd. Condition: - If number % 2 == 0 → Even - Else → Odd
Step-by-Step Algorithm
- Start the program
- Declare a variable to store the number
- Take input from the user
- Check if the number is divisible by 2 using modulo operator
- If remainder is 0, print 'Even'
- Else, print 'Odd'
- End the program
Python Program to Check Even or Odd
# Python program to check even or odd
# Taking user input
num = int(input("Enter a number: "))
# Checking condition
if num % 2 == 0:
print("The number is Even")
else:
print("The number is Odd")
Sample Output: Enter a number: 15 The number is Odd
Code Explanation
In this program, we take input from the user using the input() function and convert it to an integer using int(). We then use the modulo operator (%) to check the remainder when the number is divided by 2. If the remainder is zero, the number is even; otherwise, it is odd. The result is displayed using print().
Using Functions
def check_even_odd(n):
if n % 2 == 0:
return "Even"
else:
return "Odd"
number = int(input("Enter a number: "))
result = check_even_odd(number)
print("The number is", result)
Checking Multiple Numbers Using Loop
# Check even or odd for multiple numbers
count = int(input("How many numbers? "))
for i in range(count):
num = int(input("Enter number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
Real-World Applications
- Filtering even or odd numbers in data processing
- Used in algorithms and problem-solving tasks
- Validating input in applications
- Game development logic
- Mathematical computations and simulations
Common Mistakes to Avoid
- Forgetting to convert input to integer
- Using incorrect operator instead of modulo
- Ignoring negative numbers
- Incorrect indentation in if-else block
- Not handling invalid input
Advanced Enhancements
- Create a GUI using Tkinter
- Develop a web app using Flask
- Add error handling for invalid inputs
- Extend to check prime numbers
- Create a number classification system
Practice Exercises
- Write a program to count even and odd numbers in a list
- Create a function to return even numbers only
- Build a menu-driven number checker
- Check even/odd for numbers in a file
- Extend the program to classify numbers as prime or composite
Conclusion
The even or odd program is one of the simplest yet most important beginner exercises in Python. It introduces conditional logic and helps build problem-solving skills. By practicing variations of this program, you can strengthen your programming foundation and move toward more advanced topics.
Codecrown