Check whether the number is odd or even
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.