Q:

C++ Program to find odd or even number without using modulus operator

0

C++ Program to find odd or even number without using modulus operator

Program for finding odd or even number is one of the basic programs that every programmer must knowCurrently, we all have been doing it with the help of % operator (modulus or remainder operator). But, most of the people might not know there is another quick way to find whether a given number is odd or even.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find EVEN or ODD without using Modulus (%) Operator in C++

#include<iostream>
using namespace std;

int main() 
{
	int n; 
	
	cout<<"Enter Number: ";
	cin>>n;

	while(n>1)
	{
		n = n-2;
	}

	if(n==0)
		cout<<"Even Number"<<endl;
	else
		cout<<"Odd Number"<<endl;

	return 0;
}

Output

First Run:
Enter Number: 101 
Odd Number

Second Run:
Enter Number: 102 
Even Number

In this program, we are simply reducing the given number by 2 until it is either 0 or 1. If it is even, the end result will be 0 and if it is odd, the loop will not go to negative number, therefore the result for odd will be 1.

Then we are simply checking if number (n) is 0, it is even and if it is 1, the number is odd.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ Program to check if a number is even using Rec... >>
<< C++ program to check EVEN or ODD...