C++ Program to Check Whether a Number is EvenorOdd

We can determine whether a number is even or odd. This can be tested using different methods. The test can be done using simple methods such as testing the number’s divisibility by 2. If the remainder is zero, the number is even. If the remainder is not zero, then the number is odd. The following algorithm describes how a C++ program can test if a number is even or odd.

Example :

Number is 22
It is an even number because it is exactly divisible by 2

Number is 17
It is odd number because it is not divisible by 2

Flow Diagram:-

2+Odd-even.png

Code:-

  • Step 1. Start

  • Step 2. Enter a number.

  • Step 3. If the number is divisible by 2, it is even.

  • Step 4. If the number is not divisible by 2, it is odd.

  • Step 5. Stop

Algorithm:-

Code:-

#include <iostream>
using namespace std;
int main ()
{
    int number;
    cout << "Enter a number:"; 
    cin >> number;
 
    //checking whether the number is even or odd
    if (number % 2 == 0)
        cout << number << " : Even";
    else
        cout << number << " : Odd";
        
    return 0;
}

Output:-

Enter a number: 22

The number is even