In this article, you will learn how to write a C++ program to check whether a number is even or odd. Here I will use an if-else statement, ternary conditional statement, and modulo division to check given number is even or odd.
Before starting the program let’s first understand that even and odd numbers.
Any integer number that is exactly divisible by 2 is called an even number. For example: 12, 18, 30, 16, . . . , etc.
And the integers that are not exactly divisible by 2 are not known as odd numbers. For example 31, 7, 11, 21, . . . , etc.
By using the Modulus Operator or Bit-wise Operator you can check the even and odd numbers.
1. Modulus Operator:
The result of the % (modulus operator) is the remainder. So to check whether an integer is even or odd, the remainder is calculated by dividing the number 2 using the modulus operator %. If the remainder is zero, the integer is even otherwise odd.
2. Bit-wise Operator:
We can also check whether a number is even or odd by checking its LSB. if it is set that means the number is odd otherwise even. In the below code I will use here Bitwise And Operator to check the LSB.
If you don’t know how to check whether the bit is set or reset, read the article ” How to set, reset or toggle the bit“.
Method1: Check Whether Number is Even or Odd using if else
In the following program, we used an if..else statement that is used to check whether (data % 2) == 0 is true or not.
If the controlling expression ((data % 2) == 0) evaluates true, the data will be even otherwise it will be odd. Also, we are printing the message accordingly.
/* C++ program to check for even or odd */ #include <iostream> int main() { int data; //Get input from the user std::cout << "Enter any number: = "; std::cin >> data; //If number is divisible by 2 then //it is a even number if((data % 2) == 0) { std::cout <<data << " is even number" <<std::endl; } else { std::cout <<data << " is odd number" <<std::endl; } return 0; }
Output:
Method2: Check Whether Number is Even or Odd using ternary conditional operator
Instead of an if-else statement, you can also use the ternary conditional operator to check whether the number is even or odd.
/* C++ program to check for even or odd using conditional operator*/ #include <iostream> int main() { int data; //Get input from the user std::cout << "Enter any number: = "; std::cin >> data; //If number is divisible by 2 then //it is a even number ((data%2) == 0) ? std::cout<<data<<" is even": std::cout <<data<<" is odd"; return 0; }
Method 3: Using the bit-wise operators:
A bit-wise operator is a good solution to check even or odd numbers. You just need to check the LSB of the given integer number. Here I am checking LSB with the help of Bit-wise And Operator. If it is set that means the number is odd otherwise even.
Example-1:
Input: 9 // odd 0000000000001001 & 0000000000000001 ------------------- 0000000000000001 -------------------
Example-2:
Input: 10 //even 0000000000001010 & 0000000000000001 ------------------- 0000000000000000 -------------------
The following is an example code to check odd and even numbers using bitwise And Operator.
/* C++ program to check for even or odd using conditional operator*/ #include <iostream> int main() { int data; //Get input from the user std::cout << "Enter any number: = "; std::cin >> data; //Check LSB bit of the data (data&1) ? std::cout<<data<<" is odd": std::cout <<data<<" is even"; return 0; }