Home »
C++ programs »
C++ Most popular & searched programs
C++ program to check EVEN or ODD
Given an integer number, we have to check whether the given integer number is either EVEN or ODD using the if-else in C++.
[Last updated : February 28, 2023]
Checking EVEN or ODD using if else in C++
The numbers which are divisible by 2 are known as EVEN numbers while the numbers which are not divisible by 2 are known as ODD.
In this program, we will check whether a given number is EVEN or ODD. Here, we are checking EVEN or ODD by using three different methods.
Program to check EVEN or ODD using if else in C++
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter an integer number: ";
cin>>num;
if(num%2==0)
cout<<num<<" is an EVEN number."<<endl;
else
cout<<num<<" is an ODD number."<<endl;
return 0;
}
Output
First run:
Enter an integer number: 10
10 is an EVEN number.
Second run:
Enter an integer number: 11
11 is an ODD number.
Using Ternary Operator
(num%2==0)?
(cout<<num<<" is an EVEN number."<<endl):
(cout<<num<<" is an ODD number."<<endl);
Using Bitwise AND Operator
if(num & 0x01)
cout<<num<<" is an ODD number."<<endl;
else
cout<<num<<" is an EVEN number."<<endl;