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

Search

Wednesday, 13 March 2019

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

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;
cout<<"Please enter the number"<<endl;
cin>>n;
obj1.checknum(n);
cout<<endl;
return 0;
  }

Output 1:
Please enter the number
0
0 is an even number

Output 2:
Please enter the number
5
5 is an odd number

Output 3:
Please enter the number
7
7 is an odd number



No comments:

Post a Comment