# Check Whether Given Number is Even or Odd in C++

In this post, we will write a program to check whether the given number is even or odd in C++. We will write a program using the if-else statement and in second program we will use ternary operator to check even or odd numbers in C++. The third program we will develop using a switch case statement. At last, we will write a c++ program to check whether a number is even or odd using the function.

Condition to check even or odd number is:- If the number is divisible by 2 then it is an even number else it is an odd number. Examples of even numbers are- 4, 6, 8, 10, 12, and e.t.c. and the examples of odd numbers are- 5, 7, 9, 11, and e.t.c.

C++ program to check whether Number is Even or Odd using If-else statement

``````#include<iostream>
using namespace std;
int main()
{
// declare variable
int number;

// take input
cout << "Enter an Integer: " ;
cin >> number;

// check number
if (number % 2 == 0)
cout << number  << " is Even Number" << endl;
else
cout << number  << " is Odd Number" << endl;

return 0;
}``````

Output:-

Enter an Integer: 5
5is Odd Number

Enter an Integer: 6
6 is Even Number

## C++ Program to Check whether Number is Even or Odd Using Ternary Operators

The conditional or ternary operator is similar to the if-else statement. The if-else statement takes more than one line of the statements, but the conditional operator finishes the same task in a single statement. The operands may be an expression, constants or variables. It starts with a condition, hence it is called a conditional operator.

``````#include<iostream>
using namespace std;
int main()
{
// declare variable
int number;

// take input
cout << "Enter an Integer: " ;
cin >> number;

// check using ternary operator
(number % 2 == 0) ? cout << "Even" << endl : cout << "Odd" << endl;

return 0;
}``````

## C++ Program to Check whether Number is Even or Odd Using Switch Case

The switch case statement in C++ programming is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly. Two keywords are used in the program:- switch and case.

``````#include<iostream>
using namespace std;
int main()
{
// declare variable
int number;

// take input
cout << "Enter an Integer: " ;
cin >> number;

// check using switch case
switch(number % 2)
{
case 0 :
cout << "Even";
break;
case 1 :
cout << "Odd";
break;
}

return 0;
}``````

## Using Function

A function is a block of code that performs a specific task. For example, the main is function and every program execution starts from main function in C++ programming.

``````#include<iostream>
using namespace std;
// function to check odd even
void checkOddEven(int n)
{
if(n%2 == 0)
cout << "Even";
else
cout << "Odd";
}
// main function
int main()
{
// declare variable
int number;

// take input
cout << "Enter an Integer: " ;
cin >> number;

// check odd even
checkOddEven(number);

return 0;
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!