VOOZH about

URL: https://www.geeksforgeeks.org/cpp/cpp-program-to-check-whether-number-is-even-or-odd/

⇱ C++ Program to Check Whether Number is Even or Odd - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 5 Jun, 2026

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.

Different Methods to Check Even and Odd Numbers

A number can be checked for even or odd using different methods such as the modulo operator, bitwise AND operator, and bitwise shift operations. Each method determines whether a number is divisible by 2 based on its remainder or binary representation.

Method 1: Using Modulo Operator (%)

A number can be classified as even or odd by checking the remainder when it is divided by 2. In C++, the modulo operator (%) is used to find this remainder.

  • If number % 2 == 0, the remainder is 0, which means the number is even.
  • If number % 2 != 0, the remainder is not 0, which means the number is odd.

Output
Odd

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

Method 2: Using Bitwise AND (&) Operator

A number can also be checked for even or odd by examining its Least Significant Bit (LSB). In binary representation, odd numbers always have the last bit set to 1, while even numbers have it set to 0.

  • Perform a bitwise AND operation with 1 (number & 1) to extract the LSB of the number.
  • If the result is 1, the number is odd; if the result is 0, the number is even.

Output
Odd

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

Method 3: Using (<< and >>) Shift Operators

A number can be checked for even or odd by manipulating its Least Significant Bit (LSB) using bitwise shift operations. Right shifting and then left shifting removes the LSB from the number.

  • For an odd number, the LSB is 1, so the value changes after the shift operations and becomes different from the original number.
  • For an even number, the LSB is already 0, so the value remains unchanged after the shift operations.

Output
Odd

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

Comment