In this tutorial, we will discuss the Cpp program to check whether a number is even or odd
In this program, we are going to learn about how to find odd or even number from given number using if else statements in C++ programming language
What is Even or Odd
When the number is divided by 2 and the balance becomes zero. the above number is called as the even number – eg 2,4,6,8,10
and on the other sides when it is divided by 2 and balance becomes 1 there are called odd numbers or uneven numbers
Once This program received the number it will check the given number either odd or even.
After the program received input from the user, it is stored in the variable of num. then program divides the value of num into 2 and displays the output
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter a integer number: " << endl; cin>>num; if(num%2==0) cout<<num<<"is an even"; else cout<<num<<"is an odd"; getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer value: 44 44 is an even
case 2
Enter an integer value: 65 65 is an odd
when we use the modular operator, if it is the remainder appears as 0, it is called even number and if the remainder displays the balance as one, it is called as odd or uneven number
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter an integer number: " << endl; cin>>num; (num%2==0)? cout <<num<<" is an even number":cout<<num<<" is an odd number"; getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer value: 78 78 is an even number
case 2
Enter an integer value: 99 99 is an odd number
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter the integer number" << endl; cin>>num; if(num & 1) cout<<num<<"is an odd number"; else cout<<num<<"is an even number"; getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer value: 43 43 is an odd number
case 2
Enter an integer value: 68 68 is an even number
Program 4
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter the integer number" << endl; cin>>num; if((num /2)*2==num) cout<<num<<"is an even number"; else cout<<num<<"is an odd number"; getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer value: 88 88 is an even number
case 2
Enter an integer value: 47 47 is an odd number
Program 5
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout << "Enter the number" << endl; cin>>num; if(((num>>1)<<1)==num ){ cout<<num<<"is even "; } else{ cout<<num<<"is odd"; } getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer value: 124 88 is even
case 2
Enter an integer value: 151 151 is odd
Program 4
The same program in other languages
C program to check whether a number is even or odd
Java program to check whether a number is even or odd
Python program to check whether a number is even or odd
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…