Iseven c++: In the previous article, we have discussed about C++ Program to Swap Two Numbers. Let us learn how to Check Whether Number is Even or Odd in C++ Program.
Different methods in c++ to check whether a number is even or odd
Iseven c++: In this article, we see different methods to checks whether a number is even or odd. If we see in brief an even number is a number that is divisible by 2 while an odd number is a number that is not divisible by 2. Let see different methods one by one to check this.
- Using if-else statement and modulus(%) operator
- Using ternary operator and modulus operator(%)
- Using bitwise and operator
Method 1-Using if-else statement and modulus(%) operator
As we know that if a number is divisible by 2 then it is even otherwise it is odd. We can use the modulus(%) operator to find this. Modulus(%) operator gives the remainder when one number is divisible by another number. So if a number is even so on divisible by 2 gives remainder 0 otherwise if the number is odd it gives remainder 1. So we use this property to write our program. Let see the code for this.
#include <iostream> using namespace std; void isEven(int n) { if(n%2==0) { cout<<"Number is even"<<endl; } else { cout<<"Number is odd"<<endl; } } int main() { int num1=10, num2=9; isEven(num1); isEven(num2); return 0; }
Output
Number is even Number is odd
Here we see that when we pass number 10 in the function we get the number is even and when we pass number 9 in the program we get the number is odd. Hence our program perfectly runs.
Method 2-Using ternary operator and modulus operator(%)
Ternary operators are a short and crisp way of using if-else statements in one line.
syntax :condition ? expression1 : expression2;
This is the syntax of the ternary operator. If the condition is true then statement 1 will get executed otherwise statement 2 will get executed.
Let write code for our program.
#include <iostream> using namespace std; void isEven(int n) { n%2==0?cout<<"Number is even"<<endl:cout<<"Number is odd"<<endl; } int main() { int num1=10, num2=9; isEven(num1); isEven(num2); return 0; }
Output
Number is even Number is odd
We get the same output as we get in method 1 but this time our code is very short.
Method 3-Using bitwise and operator
In this method, we will see how can we use bitwise and operator to check whether a number is even or odd. First, let understand the logic. First, we write some numbers in binary form to analyze them.
1->1 2->10 3->11 4->100 5->101 6->110 7->111
So if we analyze the binary representation of these numbers we can see that the number which are odd have unit place as 1 in their binary representation and the numbers which even have unit place as 0 in their binary representation. So if we take bitwise and of even number, with 1 we always get output as 0 and if we take bitwise and of odd number with 1 we always get output is 1. So we can use this logic to write our program.
#include <iostream> using namespace std; void isEven(int n) { if((n&1)==0) { cout<<"Number is even"<<endl; } else { cout<<"Number is odd"<<endl; } } int main() { int num1=10, num2=9; isEven(num1); isEven(num2); return 0; }
Output
Number is even Number is odd
So these are the methods to check whether a number is even or odd in c++.