C++ program to check even or odd number

In this program we will check whether a number entered by a user is even or odd by writing some code.

To understand this example you should have the knowledge of –

  • Variables
  • Conditional statements
  • Arithmetic operators(including modulus operator)
  • Main function
using namespace std;
int main()
    int num;
    cout<<"Enter the number";
    if(num % 2 == 0)
        cout<<"The number is even";
        cout<<"The number is odd"; 
return 0;

Explanation :

  • First we have included the standard library iostream in our program so as to use the cout and cin statements. Next we have written the line to use the namespace std. The use of namespace std is explained in the previous programs and the tutorial series of C++.
  • Inside the main function, we have declared a variable of integer type named ‘num’. After that we we are allowing the user to enter a number and we are assigning that number to the variable num through cin statement. Once we got the number its time to check whether it is even or odd. We are using the if else statement for this.
  • The code inside the if block executes only if the condition mentioned is true. If the remainder obtained is 0, the condition is true and the code inside if block will execute displaying a message – “The number is even”.
  • The else block has no condition specified, and it only executes if the condition inside the if block becomes false. So, if the remainder is not zero, the else block will execute displaying a message that says “The number is odd”.

Leave a Comment

Your email address will not be published. Required fields are marked *