One of the most fundamental concepts in programming involves determining whether a number is even or odd. In this article, we’ll walk you through how to write a simple Python program to achieve this. This is a great starting point for understanding basic arithmetic operations and conditional logic in Python.
- An Even Number is an integer that is divisible by 2 without leaving a remainder. For Example: 0, 2, 4, 6, 8, 10, etc.
- An Odd Number is an integer that is not divisible by 2, leaving a remainder of 1. For Example: 1, 3, 5, 7, 9, etc.
This article discusses the most common methods, each with an explanation and example.
1. Using the Modulo Operator (%)
Using the modulo operator is a straightforward method for checking whether a number is even or odd in Python. The modulo operator (%) in Python is used to compute the remainder of the division between two numbers. When a number is divided by 2, the remainder is 0, so it’s an even number; otherwise, it is odd.
Steps:
- Get Input:
- Use input() function to get the number that you want to check.
- Use the Modulo Operator:
- Divide the number by 2 and find the remainder using the modulo operator (
%). - The modulo operator returns the remainder of a division.
- Divide the number by 2 and find the remainder using the modulo operator (
- Check the Remainder:
- If the remainder is 0, the number is even.
- If the remainder is 1, the number is odd.
- Output the Result:
- Print or return a message indicating whether the number is even or odd.
Code Example
2. Using Integer Division operator (//)
Integer Division operator (//) divides two numbers and returns the quotient by discarding the decimal part (i.e., the result is always an integer or a whole number if both inputs are integers).
For Example, (10 // 3) Output: 3 (10 ÷ 3 = 3.33; decimal part discarded).
This method uses integer division to determine whether a number is even or odd. The result of integer division is multiplied by 2 and compared with the original number.
Example
- For
number = 4:(number // 2) * 2 = (4 // 2) * 2 = 4. Since 4 equals the original number, it is even. - For
number = 5:(number // 2) * 2 = (5 // 2) * 2 = 4. Since 4 does not equal the original number, it is odd.
Code Example
Explanation
- Input: The program takes an integer input from the user.
- Integer division (
//): The expressionnumber // 2performs integer division, discarding any remainder. - Multiplication and comparison: The result of
number // 2is multiplied by 2. If the result equals the original number, it is even; otherwise, it is odd. - Output: The program prints whether the number is even or odd.

Leave a Reply