Table of Contents
Code to Check whether the number is odd or even using operators in C++
In this tutorial, we will discuss Program the Code to Check whether the number is odd or even using operators in C++
In this post, we are going to learn how to check whether the given number is odd or even using operator in C++ language
Program to Check whether the number is odd or even
Check whether the number is odd or even – using modular operator
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num;//variable declaration cout<<"Enter a integer to check odd or even\n"; //The program ask input from the user cin>>num; //input value is stored in variable num if(num%2==0){//using modular operator cout<<num<<" is even"; } else{cout<<num<<" is odd";} getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 9876 9876 is even
case 2
Enter a integer to check odd or even 8765 8765 is odd
Check whether the number is odd or even – using divisional operator
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int num;//variable declaration cout<<"Enter a integer to check odd or even\n"; //The program ask input from the user cin>>num; //input value is stored in variable num if((num/2)*2==num){//using divisional operator cout<<num<<" is even"; } else{cout<<num<<" is odd";} getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 9874 9874 is even
case 2
Enter a integer to check odd or even 1357 1357 is odd
Check whether the number is odd or even – using bitwise operator
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int num;//variable declaration cout<<"Enter a integer to check odd or even\n"; //The program ask input from the user cin>>num; //input value is stored in variable num if((num & 1)==0){//using bit wise AND operator cout<<num<<" is even number"; } else{cout<<num<<" is odd number";} getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 531 531 is odd number
case 2
Enter a integer to check odd or even 1388 1388 is even number
Check whether the number is odd or even – using ternary operator
Program 4
#include <iostream> #include <conio.h> using namespace std; int main() { int num;//variable declaration cout<<"Enter a integer to check odd or even\n"; //The program ask input from the user cin>>num; //input value is stored in variable num (num%2==0)? cout<<num<<" is even":cout<<num<<" is odd"; //using ternary operator getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 975 975 is odd
case 2
Enter a integer to check odd or even 136 136 is even
Check whether the number is odd or even – using shift operator
Program 5
#include <iostream> #include <conio.h> using namespace std; int main() { int num;//variable declaration cout<<"Enter a number to check even or odd\n"; //The program ask input from the user cin>>num; //input value is stored in variable num if((num>>1)<<1==num) //using shift operator cout<<num<<" is Even number"; else cout<<num<<" is Odd number"; getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 57 57 is odd number
case 2
Enter a integer to check odd or even 46 46 is even number
Suggested for you
Data type in C++ language
Variable in C++ language
Similar post
Java Program to check whether the number is odd or even using operators
C Program to check whether the number is odd or even using operators
C++ Program to check whether the number is odd or even using operators
Python Program to check whether the number is odd or even using operators