C++ Program To Check Whether A Number is Even Or Odd

SHARE THIS ARTICLE |

In this tutorial, I will show you that how you can make a C++ program to check whether a number is even or odd.

Program Logic and Structure:

The integers which are perfectly divisible by 2 are called even numbers like 2,4,6,8. While, the integers which are not perfectly divisible by 2 are called odd numbers like 1,3,5,7. To check for even or odd number, we will divide number by 2 and check whether the remainder of division is zero or not. To be an even number, the remainder should be equal to zero. If remainder of number is not equal to zero then it will be an odd number. In C++ language, modulus operator( % ) is used to calculate remainder of division. This operator returns the remainder as a result of division of two numbers.

C++ Code To Check Whether A Number is Even Or Odd:

#include <iostream>
using namespace std;
int main()
{
    int num;
    cout<<"ENTER NUMBER: ";
    cin>>num;
    if (num%2==0){
        cout<<num<< " IS EVEN NUMBER.";
    }
    else{
        cout<<num<< " IS ODD NUMBER.";
    }
    return 0;
}

Just press F11 to compile and execute this program. Here is the output of program:

C++ Program To Check Whether A Number is Even Or Odd

How Program works:

Below is the basic structure of a C++ program. No matter, how complicated or simple a program is, every C++ program have these lines.

#include <iostream>
using namespace std;
int main()
{
   return 0;
}

I have explained these lines fully in my post: Introduction of C++ Language & Hello World Program. I will explain rest of the lines in detail here:

Line 4:

int num;

In this line, we declared a variable named ‘num‘. This variable is used to store the number entered by user. Keyword ‘int‘ shows that this variable has integer data type. We used this type because we want to store numeric data in this variable. At the end of line, there is a semi-colon ( ; ) which terminates this statement.

Line 5:

cout<<"ENTER NUMBER: ";

In this line, we prompted the user to enter a number. cout statement is used to print something on screen. This statement prints the text which is written in double quotations( ” ” ) on the screen. In this case, it will print ” Enter a number: ” without double quotations.

Line 6:

cin>>num;

The basic purpose of cin statement is to collect input from user. This statement keeps record of keys pressed by user on key-board and stores value in the variable coming after extraction operator ( >> ). In this case, it will save the number entered by user in variable ”num‘. Lets assume that number entered by user is 8.

Line 7:

if (num%2==0){

If…Else statement starts from this line. This statement helps us to make decision. Using this statement we can make decision whether a number is even or odd. There is an expression ‘num%2==0‘ in this line, if this expression becomes true the statement of Line 8 would be executed and if this expression becomes false then statement of Line 10 would be executed. As we assumed the number entered by user is 8, if we replace ‘num‘ by 8 in this expression then expression would become true. Because ( 8%2==0 ) evaluates to ( 0==0 ) which is true , so statement of Line 8 would be executed.

Recommended reading: C++ Program To Find Area of Triangle Using Heron’s Formula

Line 8:

cout<<num<< " IS EVEN NUMBER.";

This line is used to tell the user that number he entered is an even number. Insertion operator( << ) is used two times in this line, once for variable output and once for sentence output. It should be noted that keyword ‘num‘ must NOT be included in double quotations. When the program will show output then ‘num‘ would be replaced by number stored in the variable Like 8.

Line 9:

 }

In this line, we have only a closing curly brace. The statements between curly braces after expression are considered true block.Here this brace shows that statements of true block end here.

Line 10:

else{

This is the ‘else‘ part of if…else statement. If expression becomes false then the statement coming after keyword ‘else‘ are executed. If expression is true then this part remains idle.

Line 11:

cout<<num<< " IS ODD NUMBER.";

This line is executed only when expression is false. The falsity of expression shows that number entered by user is an odd number. Therefore this statement is used to inform the user that number is odd number. If expression is true then this line would not be executed.

Line 12:

 }

Here the closing curly  brace shows that statements of false block end here.

This was all about C++ program to check whether a number is even or odd.

Thanks for reading this article. If you have any question then you can ask me in comments.

You can also Click Here to Download Exercise Files.