Home C++ Check if a number is even or odd in C++

Check if a number is even or odd in C++

In this article we show you how to check if a number is even or odd in C++

We use the modulus operation which returns the remainder obtained when number is divided by 2.

For an even number, the remainder obtained on dividing a number by 2 has to be 0. For Odd, the remainder has to be 1.

Example

 

#include <iostream>
using namespace std;

int main()
{

    cout << "Even or Odd \n\n";

    //variable declaration
    int number;

    //taking input from the command line
    cout << " Enter the number that you want to check : ";
    cin >> number;

    //check if the number is even or odd
    if(number % 2 == 0)
    {
        cout << "\n\nThe entered number "<< number << " is Even\n";
    }
    else
    {
        cout << "\n\nThe entered number "<< number << " is Odd\n";
    }

    cout << "\n\n";

    return 0;
}

 

You may also like