Home » C++ programs » C++ Most popular & searched programs

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

Submitted by Abhishek Pathak, on April 06, 2017

This program will read an integer number and check whether it is EVEN or ODD without using Modulus (%) Operator.

Program for finding odd or even number is one of the basic programs that every programmer must know. Currently, 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.

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.