Friday, 21 September 2018

Checking whether a number is even or odd in C++

Even and odd numbers

An even number is a number which gives remainder 0 when divided by 2, i.e. it is divisible by 2.
An odd number is a number which gives remainder 1 when divided by 2, i.e. it is not divisible by 2.
In C++  the mod(%) operator is used to extract the remainder.

Write a program in C++ to enter a number and check whether it is even or odd


We need to input a number from the user and check whether it is even or odd. We need to input a number with the help of a variable, check whether it is even or odd. Then the result must be displayed on the screen.

Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
cout<<"Enter any number=";
cin>>a;
if(a==0)
cout<<"\n The number is zero";
else if(a%2==0)
cout<<"\n The number is even";
else
cout<<"\n The number is odd";
getch();
}


Output

Even and odd number differentiation
CHECKING EVEN OR ODD NUMBER









Explanation


The cin operator is used to assign the entered value into a variable. In the above program, the input number 4 is assigned to variable a
The mod(%) is an arithmetic operator which returns an integer type value. In the above output 
a=4, which is even number so the mod operator returns 0.
  

  

No comments:

Post a Comment