Table of Contents
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 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
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
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.