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

In this program, we will learn how to check whether a number is even or odd using C++ programming language.

An integer number which is perfectly divisible by 2 is called even number. For example, 2, 4, 6, 8, .... are even numbers.
An integer number which gives a remainder when divided by 2 is known as an odd number. For example, 3, 5, 7, 9, .... are odd numbers.

We will discuss two ways

1. Using Modulus operator ( % )
2. Using Bitwise operator ( & )

Let's discuss them one by one.

## Check whether a number is even or odd

### Using modulus operator ( % )

In the below code, the variable num is divided by 2 and its remainder is compared. If the remainder is 0, then the number is even. If the remainder is 1, then the number is odd.

Code:-

```#include <iostream>

using namespace std;

int main(){
int num;

cout << "Enter a number: ";
cin >> num;

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

return 0;
}```

Output:-

Enter a number: 9
9 is an odd number.
Enter a number: 22
22 is an even number.

### Using Bitwise operator ( & )

A number is odd if it has 1 as its rightmost bit in bitwise representation. And it is even if it has 0 as its rightmost bit in bitwise representation. This can be found by using bitwise AND (&) operator on the number and 1. If it gives 0, then the number is even and if it gives 1, then the number is odd.

Code:-

```#include <iostream>

using namespace std;

int main(){
int num;

cout << "Enter a number: ";
cin >> num;

if ( (num & 1) == 0)
cout << num << " is an even number." << endl;
else
cout << num << " is an odd number." << endl;

return 0;
}```

Output:-

Enter a number: 65
65 is an odd number.
Enter a number: 58
58 is an even number.

### You can learn about many other C++ Programs Here.

#### Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below. #### Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development. 