The concept of odd and even numbers is one of the most basic and important ideas in mathematics.
Odd numbers are those that cannot be divided by 2 without leaving a remainder (e.g., 1, 3, 5, 7, 9, etc.),
whereas even numbers are divisible by 2 (e.g., 2, 4, 6, 8, 10, etc.).
Objective
The goal of this program is to determine if a given number is odd or even. The program will take an integer input from the user, check whether it is divisible by 2, and output whether the number is “Odd” or “Even”.
Python Code
def check_odd_or_even():
# Take input from the user
number = int(input("Enter a number: "))
# Check if the number is divisible by 2
if number % 2 == 0:
print(f"{number} is Even.")
else:
print(f"{number} is Odd.")
# Call the function to execute
check_odd_or_even()
Explanation of the Program Structure
This Python program is structured as follows:
- Function Definition: We define a function called
check_odd_or_even()
.
This function contains the logic for checking whether the number is odd or even. - User Input: The program takes an integer input from the user using the
input()
function.
Theinput()
function returns a string, so it is converted into an integer usingint()
. - Modulo Operator: We use the modulo operator (
%
) to check whether the number is divisible by 2.
If the remainder is 0 (i.e.,number % 2 == 0
), the number is even. If not, the number is odd. - Output: Based on the result of the condition, the program prints whether the number is “Even” or “Odd”.
- Calling the Function: The function is called at the end of the program to execute the logic.
How to Run the Program
To run this program:
- Install Python on your system from here (if you haven’t already).
- Open any text editor (such as Notepad, VS Code, Sublime Text, etc.) and paste the Python code into a new file.
- Save the file with a
.py
extension, for example,odd_or_even.py
. - Open a terminal or command prompt on your computer.
- Navigate to the folder where you saved the Python file and type:
python odd_or_even.py
- The program will prompt you to enter a number. After entering a number, it will display whether the number is “Odd” or “Even”.
Example
Input:
Enter a number: 7
Output:
7 is Odd.