C++ Program to Check Whether Number is Even or Odd

Program to check whether a number is even or odd

Here we will discuss how to check whether a number is even or odd in C++ programming language.

  • A number is even if it is divisible by 2
  • To check this we use if else statement along with the % operator.
  • If the condition is true i.e. number is divisible by 2 then the number is even
  • Otherwise the number is odd

Working:

  • User gives an input.
  • Input is stored in an int type variable say prep.
  • prep is then checked using the if else statement with the condition

if(prep%2==0)

  • If the above mentioned condition is true then the number is an even number.
  • Otherwise the number is an odd number.
Program to check A number is even or odd

C++ Code:

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

Output:

Enter a number: 97
97 is an odd number

Please Login/Signup to comment