C++ Program to Check Whether Number is Even or Odd

by | Jan 7, 2023 | C++, C++ Programs

Introduction

This program will check whether a number entered by the user is even or odd.

An even number is one that is exactly divisible by 2. On the other hand, an odd number is one that is not exactly divisible by 2.

In this program, we will use this logic to check whether a number is even or odd.

To write this program, you should know about the following C programming concepts:

1. Operators

2. if-else statement

3. nested if-else

Program

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter an integer: ";
cin>>n;


if(n%2==0) // condition to be an even number
{
cout<<n<<"is a even number."<<endl;
}
else // otherwise the number is odd
{
cout<<n<<" is a odd number."<<endl;
}
return 0;
}

 

Output

Explanation

‘n’ is a variable whose value will be entered by the user.

if(n%2==0)

here we will check whether the remainder obtained by the division of n by 2 gives 0 or not. If it results true, then the body of the ‘if’ statement will be executed.

If the above condition is false, then the body of the else part is executed.

In the output section, the value of ‘n’ is 2568, which is an even value (since 2568%2==0), thus the body of the ‘if’ statement is executed.

 

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.