Examine if a quantity is even or odd in C++ Program
On this brief tutorial collection of C++, we’ll present you find out how to examine whether or not a quantity entered by the person is Even or Odd.
- The quantity which could be divided by 2 is even
- The quantity that may’t be divided by 2 is odd
First Program – Utilizing % operator in if else
The % operator is used to return the rest after division. As such, the rest of a quantity that may be divided by two is all the time zero – we are able to use it to attain our process i.e. a quantity enter by the person is even or odd.
See the C++ program under that makes use of the % operator with if..else statements. For taking person enter, we used C++ cin as proven in this system under:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#embody <iostream> utilizing namespace std;
int fundamental() { int quantity;
//Asking person enter to examine for even or odd
cout << “Please enter a quantity? “; cin >> quantity;
//Utilizing if with % operator to examine if quantity is even or odd
if ( quantity % 2 == 0) cout <<“The given quantity “ << quantity << ” is EVEN!” <<“nn”; else cout <<“The given quantity “ << quantity << ” is ODD!” <<“nn”;
return 0;
} |
Pattern Output 1:
Pattern Output 2
Second Method – Utilizing Bitwise operator AND
First, take a look at this C++ program that makes use of AND bitwise operator to examine if the quantity is Even or Odd:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#embody <iostream>
utilizing namespace std;
int fundamental() {
int quantity;
//Asking person enter to examine for even or odd
cout << “Please enter a quantity? “;
cin >> quantity;
//Utilizing bitwise operator AND
if((quantity & 1) == 0)
cout <<“The given quantity ” << quantity << ” is EVEN!” <<“nn”;
else
cout <<“The given quantity “ << quantity << ” is ODD!” <<“nn”;
return 0;
} |
Pattern output 1
Pattern output 1
Third Method – Utilizing ternary operator instance
This program additionally tells if the entered quantity is even or odd by utilizing the ternary operator. Take a look on the code and pattern outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#embody <iostream> utilizing namespace std;
int fundamental() {
int quantity;
//Asking person enter to examine for even or odd cout << “Please enter a quantity? “;
cin >> quantity;
//Ternary operator to examine a quantity is even or odd
(quantity % 2 == 0) ? cout << quantity << ” is an EVEN quantity!” : cout << quantity << ” is and ODD quantity!”;
return 0;
} |
Output:
Fourth Resolution – Utilizing Swap Case Assertion
As such, the swap case can be a decision-making assertion in C++. We embody this instance simply so as to add another choice in the event you get this task.
This instance additionally makes use of the modulus operator %), identical to with the if assertion and the rest choices are used as circumstances.
See the code and pattern output under:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#embody <iostream>
utilizing namespace std;
int fundamental(){
int quantity;
//Asking person enter to examine for even or odd
cout << “Please enter a quantity? “;
cin >> quantity;
swap(quantity % 2)
{ case 0: cout <<“The given quantity “ << quantity << ” is EVEN!” <<“nn”; break; case 1: cout <<“The given quantity “ << quantity << ” is ODD!” <<“nn”; break; } return 0;
} |
Output: