C++ program to check given number is even or odd. - coderforcoders

## Wednesday, 13 March 2019

### Even or odd number in C++

#### Theory:

Even or odd in C++ is a simple C++ program to check a given number is Even or odd. A user will enter an number and the program will check whether the given number is Even or odd and outputs the result. evenOrOdd is a class declared and int checknum() is the member function which is used to check whether the given number is Even or odd number.

#### Logic:

In this program we are using the concepts of class and objects. Even numbers are the integer numbers which can be completely divided by the number 2 example 4, 0, 560 etc. Odd numbers are the integer numbers which can not be completely divided by 2 example 3, 5 27 etc. To check that we are using a simple if statement in our program.

if(n%2==0)
cout<<n<<" is an even number ";
else
cout<<n<<" is an odd number ";

#### Program:

C++ program to check given number is even or odd.

#include<iostream>
using namespace std;
class evenOrOdd{
public:
void checknum(int n)
{
if(n%2==0)
{
cout<<n<<" is an even number ";
}
else
{
cout<<n<<" is an odd number ";
}
}
};
int main()
{
evenOrOdd obj1;
int n;
cin>>n;
obj1.checknum(n);
cout<<endl;
return 0;
}

Output 1:
0
0 is an even number

Output 2: