cplusplus
cplusplus

 

 

Introduction

In C++, determining if a given number is odd or even is a basic but important operation. This is often one of the first programs you write when learning a new programming language. An even number is divisible by 2 with no remainder, while an odd number leaves a remainder of 1 when divided by 2.

The objective of this program is to write a simple C++ program that takes an integer as input from the user and determines whether the number is odd or even using basic conditional statements.

Objective

The objective of this task is to implement a C++ program that:

  • Accepts an integer input from the user.
  • Uses the modulus operator to check if the number is divisible by 2.
  • Prints “Even” if the number is divisible by 2 and “Odd” if it is not.

Code Implementation

#include 
using namespace std;

int main() {
    int number;

    // Ask the user for input
    cout << "Enter an integer: "; cin >> number;

    // Check if the number is even or odd using modulus operator
    if (number % 2 == 0) {
        cout << "The number " << number << " is Even." << endl;
    } else {
        cout << "The number " << number << " is Odd." << endl;
    }

    return 0;
}

Explanation of the Program

Let’s break down the code and understand how it works:

  1. Include the header file:
    #include <iostream> is used to include the standard input-output stream, which allows us to use cin for input and cout for output.
  2. Define the main function:
    The main() function is the entry point of any C++ program. It is where the program starts executing.
  3. Declare a variable:
    int number; declares an integer variable to store the number input by the user.
  4. Get input from the user:
    cin >> number; prompts the user to input an integer, which is then stored in the variable number.
  5. Check for even or odd:
    The if (number % 2 == 0) condition uses the modulus operator to check if the remainder when dividing number by 2 is zero (i.e., if the number is divisible by 2). If the condition is true, the number is even; otherwise, it’s odd.
  6. Output the result:
    cout << "The number " << number << " is Even." << endl; displays whether the number is even or odd.
  7. Return 0: The return 0; statement marks the successful completion of the program.

How to Run the Program

Follow these steps to run the program:

  1. Write the code in a C++ editor or an Integrated Development Environment (IDE) like Code::Blocks, Visual Studio, or CLion.
  2. Save the file with a .cpp extension, for example, OddEven.cpp.
  3. Compile the program. If you’re using a command-line compiler like g++, you can compile the code with:
    g++ OddEven.cpp -o OddEven
  4. Run the compiled program by typing:
    ./OddEven
  5. Enter a number when prompted, and the program will tell you whether it is odd or even.

Conclusion

This program demonstrates how to use basic conditional statements and the modulus operator to determine whether a number is odd or even. It serves as a foundation for learning more complex operations and algorithms in C++ programming.

 

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 :)