C++ code to 5 ways to check whether the given integer is Even or Odd
- Home
- Check value
- C++ code to 5 ways to check whether the given integer is Even or Odd
- On
- By
- 0 Comment
- Categories: Check value
C++ code to 5 ways to check whether the given integer is Even or Odd
C++ code to 5 ways to check whether the given integer is Even or Odd
In this tutorial, we will discuss the concept of C++ code to 5 ways to check whether the given integer is Even or Odd
In this post, we are going to learn how to check whether the given integer is even or odd using 5 different ways in C++ programming language.
C++ code to 5 ways to Check Even or Odd
Using Modular operator
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter a integer number" << endl; cin>>num;//takes input from user //using modular operator if(num%2==0){ cout<<num<<" is a Even number "; }else{ cout<<num<<" is a odd number "; } getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter a integer number 500 500 is a Even number
case 2
Enter a integer number 301 301 is a odd number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using modular operator
Using the division operator
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter the integer number" << endl; cin>>num;//takes input from user //using division operator if((num/2)*2==num){ cout<<num<<" is a Even number "; }else{ cout<<num<<" is a odd number "; } getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter a integer number 300 300 is a Even number
case 2
Enter a integer number 211 211 is a odd number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using division operator
Using Bitwise AND operator
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter the integer number" << endl; cin>>num;//takes input from user //using bitwise AND operator if((num&1)==0){ cout<<num<<" is a Even number "; }else{ cout<<num<<" is a odd number "; } getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter a integer number 788 788 is a Even number
case 2
Enter a integer number 555 555 is a odd number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using bitwise AND operator
Using shift operator
Program 4
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter the integer number" << endl; cin>>num;//takes input from user //using shift operator if(((num>>1)<<1)==num){ cout<<num<<" is a Even number "; }else{ cout<<num<<" is a odd number "; } getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter a integer number 1000 1000 is a Even number
case 2
Enter a integer number 999 999 is a odd number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using shift operator
Using ternary operator
Program 5
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter the integer number" << endl; cin>>num;//takes input from user //using ternary operator (num%2==0) ? cout<<num<<" is even.":cout<<num<<" is odd."; getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter a integer number 50 50 is Even
case 2
Enter a integer number 51 51 is odd
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using the ternary operator
Suggested for you
Similar post
Program to find whether a Number is Prime or Not in C++
Program to find whether a Number is Prime or Not in C
Program to find whether a Number is Prime or Not in Java
Program to find whether a Number is Prime or Not in Python
C++ program to find first n prime numbers
Java program to find first n prime numbers
C program to find first n prime numbers
C code to 5 ways to check whether the given integer is Even or Odd
Java code to 5 ways to check whether the given integer is Even or Odd
C++ code to 5 ways to check whether the given integer is Even or Odd
Python code to 5 ways to check whether the given integer is Even or Odd