Python
Python

 

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:

  1. 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.
  2. User Input: The program takes an integer input from the user using the input() function.
    The input() function returns a string, so it is converted into an integer using int().
  3. 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.
  4. Output: Based on the result of the condition, the program prints whether the number is “Even” or “Odd”.
  5. 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:

  1. Install Python on your system from here (if you haven’t already).
  2. Open any text editor (such as Notepad, VS Code, Sublime Text, etc.) and paste the Python code into a new file.
  3. Save the file with a .py extension, for example, odd_or_even.py.
  4. Open a terminal or command prompt on your computer.
  5. Navigate to the folder where you saved the Python file and type:
    python odd_or_even.py
  6. 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)