Open In App

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

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A number is even if it is completely divisible by 2 and it is odd if it is not completely divisible by 2. In this article, we will learn how to check whether a number is even or odd in C++.

Examples

Input: n = 11
Output: Odd
Explanation: Since 11 is not completely divisible by 2, it is an odd number.

Input: n = 20
Output: Even
Explanation: Since 20 is completely divisible by 2, it is an even number.

Check if a Number is Even or Odd in C++

The simplest method to determine if a given number is even or odd is by checking the remainder left when the number is divided by 2. The modulo operator (%) in C++ returns remainder after the division. So, using an if-else or any other conditional statement, we check if the remainder of the number divided by 2 is 0. If it is 0, the number is even. Otherwise, the number is odd.

Code Implementation

C++
// C++ program to check if the number is even
// or odd using modulo operator
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n = 11;

    // If n is completely divisible by 2
    if (n % 2 == 0)
        cout << "Even";

    // If n is NOT completely divisible by 2
    else
        cout << "Odd";
    return 0;
}

Output
Odd

Time Complexity: O(1)
Auxiliary Space: O(1)

Other Methods to Check if the Number is Even or Odd

The above method is the simplest method to check the parity of the number. But there are two more different ways to check whether the given number is a prime number or not.

Using Bitwise AND (&) Operator

We can also check the parity of the number just by checking the Least Significant Bit (LSB) of the number. In binary representation of odd numbers, the LSB (0th bit) in an odd number is always set i.e. will always be 1. So, we can extract the LSB by doing bitwise AND (&) with 1 and check whether it is set or not.

C++
// C++ program to check if the number is even
// or odd using bitwise (&) operator
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n = 11;
  
    // Performing AND operation of n with 1
    int res = n & 1;

    // If res is 0, the number is even
    if (res == 0)
        cout << "Even";

    // Otherwise, number is odd
    else
        cout << "Odd";
    return 0;
}

Output
Odd

Time Complexity: O(1)
Auxiliary Space: O(1)

Using (<< and >>) Shift Operators

The least significant bit (LSB) in an odd number is always set, i.e., 1. If we right shift the number by one and then left shift it by one, the LSB will become 0, making it unequal to the original number. In contrast, if we perform the same operation on an even number, it won’t change because the LSB is already 0 from the start.

C++
// C++ program to check if the number is even
// or odd using shift (<< and >>) operators
#include <iostream>
using namespace std;

int main() {
    int n = 11;
  	
  	// Variable to store the original number
    int temp = n;
  
  	// Right shift the number by 1
    temp = temp >> 1;
  	
  	// Left shift the number back by 1
    temp = temp << 1;
  
  	// Check if the value of the number changed
  	// or not
    if (temp == n) {
        cout << "Even" << endl;
    } else {
        cout << "Odd" << endl;
    }
    return 0;
}

Output
Odd

Time Complexity: O(1)
Auxiliary Space: O(1)



Practice Tags :

Similar Reads