VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-given-number-even-odd/

⇱ Check Even or Odd - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check Even or Odd

Last Updated : 9 Apr, 2026

Given a number n, check whether it is even or odd. Return true for even and false for odd.

Examples:

Input: n = 15
Output: false
Explanation: 15 % 2 = 1, so 15 is odd .

Input: n = 44
Output: true
Explanation: 44 % 2 = 0, so 44 is even.

[Naive Approach] By Finding the Remainder - O(1) Time and O(1) Space

We can check the remainder when divided by 2. If the remainder is 0, the number is even, otherwise it is odd. For example, when we divide 13 by 2, we get remainder as 1 and when we divide 14 by 2, we get remainder as 0.


Output
false

[Efficient Approach] Using Bitwise AND Operator - O(1) Time and O(1) Space

The last bit of all odd numbers is always 1, while for even numbers it’s 0. So, when performing bitwise AND operation with 1, odd numbers give 1, and even numbers give 0.

Note: Bitwise operators are extremely fast and efficient because they operate directly at the binary level, making them significantly faster than arithmetic or logical operations.

Examples:

15 -> 1 1 1 1
& 0 0 0 1
-------
0 0 0 1 , so this we can say it is an odd number.

44 -> 1 0 1 1 0 0
& 0 0 0 0 0 1
----------
0 0 0 0 0 0 , so this we can say it is an even number.


Output
false
Comment
Article Tags: