C++ Program to check whether the number is even or odd using division operator [DEVCPP/GCC]


The division of odd numbers with 2 will always result in a fractional value. For example, 25/2=12.50. The fractional part of the result is ignored by the compiler in case of integer datatype. Hence on multiplying the result with 2, we would not get the original number.

For instance,
55/2 = 27.5 = 27
27*2 = 54

Here, we get 54 after calculation which signifies 55 is an odd number.

// Program to check whether the number is even or odd using division operator


#include<iostream>

using namespace std;

int main()
{

int num;

cout<<"ENTER THE NUMBER TO BE CHECKED:\n";
cin>>num;

if( (num/2) * 2  == num)       // Using division operator
{
cout<<"NUMBER IS EVEN\n";
}
  else
{
cout<<"NUMBER IS ODD";
     }
 
return 0;
}



Share this

Related Posts

FIND US ON FACEBOOK!