beginnerNumber ProblemsJava

Check Whether a Number Is Even or Odd Without the Modulus Operator

Classify a number as even or odd without using the modulus operator.

Quick Answer

The last bit of a binary number decides its parity: it is 0 for even numbers and 1 for odd. So n & 1 is 0 for even and 1 for odd, with no modulus needed. For 10 (binary 1010) the result is 0, so 10 is even.

Problem Statement

Given an integer n, decide whether it is even or odd without using the modulus operator %. Return the label "Even" or "Odd".

For example, 10 is even and 7 is odd. The trick is that the lowest bit of a number's binary form is 0 for even values and 1 for odd values.

Input: A single integer n.

Output: The string "Even" or "Odd".

Examples

Example 1
Input:  10
Output: Even

10 in binary is 1010; its lowest bit is 0, so it is even.

Example 2
Input:  7
Output: Odd

7 in binary is 0111; its lowest bit is 1, so it is odd.

Constraints

  • n fits in a 32-bit int and may be negative
  • The modulus operator % may not be used

Think Before You Code

Reveal the questions to ask yourself first
  • What does the lowest bit of a binary number tell you about parity?
  • Which bitwise operator isolates just that lowest bit?
  • Does the bit trick still work for negative numbers in two's complement?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Every even number ends in a 0 bit; every odd number ends in a 1 bit.
Hint 2
Bitwise AND with 1 keeps only the lowest bit: `n & 1`.
Hint 3
If `n & 1 == 0` the number is even, otherwise it is odd.

Approach

Reveal the step-by-step approach

Read the parity directly from the lowest bit.

  1. Compute n & 1, which masks out every bit except the least significant one.
  2. If the result is 0, return "Even".
  3. Otherwise return "Odd".

This works for negative numbers too: in two's complement the lowest bit still encodes parity, so -3 & 1 is 1 (odd).

Dry Run

Walk through the example step by step

Checking n = 10:

n     = 10 = 1010 (binary)
mask  =  1 = 0001 (binary)
n & 1 = 1010 & 0001 = 0000 = 0
result: 0 means "Even"

Solution

Reveal the full Java solution
public class EvenOrOdd {
    public static String checkEvenOdd(int n) {
        if ((n & 1) == 0) {
            return "Even";
        }
        return "Odd";
    }

    public static void main(String[] args) {
        System.out.println(checkEvenOdd(10)); // Even
        System.out.println(checkEvenOdd(7));  // Odd
    }
}

The value of a binary number's least significant bit is exactly 1 when the number is odd and 0 when it is even, because every higher bit contributes a multiple of two. Masking with & 1 extracts that bit in constant time, avoiding the forbidden % operator, and the two's-complement encoding keeps the rule true for negatives.

Time: O(1)Space: O(1)

Common Mistakes

  • Writing `n & 1 == 0` without parentheses — `==` binds tighter, so it must be `(n & 1) == 0`.
  • Assuming the trick fails for negatives; it works in two's complement.

Edge Cases to Test

  • n = 0 has a lowest bit of 0, so it is even.
  • Negative odd numbers like -7 still return "Odd".

Interview Follow-Ups

  • How could you decide parity using division by 2 and multiplication instead?
  • How would you extend this to test divisibility by 4 using bit masks?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us