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

This program will read an integer number and check whether it is EVEN or ODD without using Modulus (%) Operator.
Submitted by Abhishek Pathak, on April 06, 2017 [Last updated : February 28, 2023]

Finding the 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.



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.