**C++ programs to check if a number is even or odd** have been shown here. An integer number $n$ is said to be even if it is divisible by $2$ i.e. $(n\mod 2 = 0)$ and it is called odd if it is not divisible by 2 i.e. it does not result in 0 as the remainder when divided by $2$. So a number in the form of $2n$, where $n$ is an integer, is called even and a number in the form of $2n+1$ or $2n-1$ is called odd. For example $... -4, -2, 0, 2, 4$ ... etc. are even numbers and $... -3, -1, 1, 3, 5$ ... etc. are odd numbers.

Another approach is to apply bitwise AND operator (&) to check if $n$ is even or odd. We need to perform bitwise AND operation between $n$ and $1$. If $n$ AND $1 = 0$, then $n$ is even and if $n$ AND $1 = 1$, then $n$ is odd.

Page content(s):

**4. Program & output: Modulo operation**

**5. Program & output: Bitwise AND operation**

Additional content(s):

## 1. Algorithm to check if a number is even or odd

// $n$ is an integer number as input//

1. Check if $n \mod 2 = 0$, then Return *Even* else

2. Return *Odd*

## 2. Pseudocode to check if a number is even or odd

**Input**: An integer number $n$

**Output**: If $n$ is even or odd

1. **Procedure** evenOrOdd($n$):

2. **If** $n\mod2==0$:

3. **Return ***even*

4. **Else:**

5. **Return ***odd*

6. **End Procedure**

## 3. Time complexity to check if a number is even or odd

**Time Complexity: ****O(1)**

## 4. C++ Program to check if a number is even or odd

/************************************************ alphabetacoder.com C++ program to check if a number is even or odd *************************************************/ #include <iostream> using namespace std; int main() { // declare variable int n; //take input cout << "Enter the number = "; cin >> n; //check if n is even or odd if (n % 2 == 0) cout << n << " is even"; else cout << n << " is odd"; return 0; }

Output

**Case 1:**

Enter the number = -5

-5 is odd

**Case 2:**

Enter the number = 8

8 is even

**Case 3:**

Enter the number = 3

3 is odd

## 5. C++ Program to check if a number is even or odd using bitwise AND operation

/*************************************** alphabetacoder.com C++ program to check if a number is even or odd using bitwise AND operation ****************************************/ #include <iostream> using namespace std; int main() { // declare variable int n; //take input cout << "Enter the number = "; cin >> n; //check if n is even or odd // by doing bitwise AND operation // with 1. If the result is 0 then // n is even. If the result is 1 // then n is odd. if (n & 1) cout << n << " is odd"; else cout << n << " is even"; return 0; }

Output

**Case 1:**

Enter the number = 15

15 is odd

**Case 2:**

Enter the number = 22

22 is even

**Case 3:**

Enter the number = 13

13 is odd