Placement Prep

Check Odd or Even Without Modulus: 3 Python Methods

Three Python approaches to check odd or even without the modulus operator: bitwise AND, integer division, and flag toggling, with runnable code.

By FACE Prep Team 4 min read
python odd-even bitwise-operators placement-prep coding-interview python-programs number-classification

Placement coding rounds sometimes ask for an odd/even check without the % operator, testing whether you know alternatives to the most obvious solution.

The modulus operator (%) is the standard Python approach: n % 2 == 0 returns True for even numbers. Interviewers occasionally constrain it away to see whether you know about bitwise operations, integer arithmetic, and loop-based logic. This article covers all three alternative methods with runnable Python code and edge-case notes.

Why Interviewers Ask This in Coding Rounds

Three reasons show up in placement preparation contexts:

  • Operator constraints: Competitive programming problems and low-level coding challenges sometimes restrict which operators you can use. Bitwise-only solutions appear in embedded systems contexts and certain online judges.
  • Testing bitwise knowledge: Bitwise operations appear in coding sections of placement assessments including TCS NQT and AMCAT. Knowing the & operator’s behaviour goes beyond this specific problem and into a broader category of bit-manipulation questions.
  • Understanding number representation: The bitwise method works because of how integers are stored in binary. Understanding that connection builds a foundation for number-theory problems that come up repeatedly in placement prep.

None of the alternatives run faster than n % 2 in practical Python code; the difference is negligible for the input sizes that appear in coding rounds. The exercise is about knowing why each method works.

Method 1: Using the Bitwise AND Operator

Every integer stored in memory has a binary representation. Odd numbers always have a 1 in the least significant bit (LSB); even numbers always have a 0.

The & operator performs a bitwise AND on two integers. Masking any number against 1 isolates the LSB, which directly answers the odd/even question.

Worked examples:

  • 5 in binary is 101. 5 & 1 = 001 = 1. Result: Odd.
  • 4 in binary is 100. 4 & 1 = 000 = 0. Result: Even.

According to the Python 3 documentation on bitwise operations, the & operator computes bitwise AND: each bit in the result is 1 only when the corresponding bits in both operands are 1.

def check_odd_even_bitwise(n):
    if n & 1:
        return "Odd"
    return "Even"

print(check_odd_even_bitwise(5))   # Odd
print(check_odd_even_bitwise(4))   # Even
print(check_odd_even_bitwise(-5))  # Odd
print(check_odd_even_bitwise(-4))  # Even

Edge Case: Negative Numbers

Python’s bitwise AND extends to negative integers using their two’s complement representation. -5 & 1 evaluates to 1 (odd) and -4 & 1 evaluates to 0 (even). Both are correct. The bitwise method handles negatives without special-casing.

Method 2: Integer Division and Back-Multiplication

This approach uses Python’s floor-division operator // paired with multiplication:

  • Divide n by 2 using integer division: n // 2
  • Multiply the result back by 2
  • If the product equals the original n, the number is even; otherwise, it is odd

The core logic: integer division discards any remainder. For an odd number, dividing by 2 loses that remainder, so multiplying back will not reproduce the original value.

Worked examples:

  • 5 // 2 = 2. 2 * 2 = 4. 4 does not equal 5. Result: Odd.
  • 4 // 2 = 2. 2 * 2 = 4. 4 equals 4. Result: Even.

The Python 3 documentation on arithmetic operations specifies that // performs floor division, rounding toward negative infinity rather than toward zero.

def check_odd_even_division(n):
    if (n // 2) * 2 == n:
        return "Even"
    return "Odd"

print(check_odd_even_division(5))   # Odd
print(check_odd_even_division(4))   # Even
print(check_odd_even_division(-5))  # Odd
print(check_odd_even_division(-4))  # Even

Edge Case: Negative Numbers

Python’s floor division rounds toward negative infinity, not toward zero. For -5: -5 // 2 = -3 (not -2), and -3 * 2 = -6, which does not equal -5. The method correctly identifies -5 as odd. For -4: -4 // 2 = -2, and -2 * 2 = -4, which equals -4. Correctly identified as even. No special handling needed.

Method 3: Flag Variable Toggle

This method uses a boolean flag and a loop:

  • Set flag = True
  • Loop n times, flipping flag on each iteration with flag = not flag
  • After n flips: if n is even, flag returns to True; if n is odd, flag ends at False

This works because toggling a boolean twice returns it to its starting state. Starting from True:

  • After 4 flips: True, False, True, False, True. Final value: True. Result: Even.
  • After 5 flips: True, False, True, False, True, False. Final value: False. Result: Odd.
def check_odd_even_flag(n):
    flag = True
    for _ in range(n):
        flag = not flag
    return "Even" if flag else "Odd"

print(check_odd_even_flag(5))  # Odd
print(check_odd_even_flag(4))  # Even

Edge Case: Negative Numbers

range() with a negative argument returns an empty sequence in Python. Calling check_odd_even_flag(-5) loops zero times, leaves flag = True, and returns "Even", which is incorrect for -5. This method is valid only for non-negative integers.

Time Complexity

The flag toggle runs in O(n) time. The loop executes once per unit of input, making the function unusable for large values: check_odd_even_flag(1000000) runs the loop one million times before returning. The bitwise and division methods both run in O(1). The flag approach has no practical production use; its value is purely in illustrating how loop-based toggling maps to parity.

Comparing the Three Methods

MethodTime ComplexityHandles Negative NumbersBest Use
Bitwise AND (n & 1)O(1)YesInterviews, performance-sensitive contexts
Integer division ((n // 2) * 2)O(1)YesReadable alternative, beginner practice
Flag toggleO(n)NoEducational only — do not use in production

For placement coding rounds, the bitwise AND method is the answer to memorise. It is also the response most interviewers expect when they say “without modulus.” The integer division method is a solid second answer and easier to explain intuitively. The flag toggle method is a conversation-starter about time complexity, not a submission-ready solution.

Other Python number-property checks follow similar patterns. To extend this work to multi-digit number properties, see how to check if a number is Armstrong. For a broader set of similar problems, the Python practice programs collection covers comparable conditional-logic patterns. For the basic comparison structure underlying all three methods here, finding the greatest of two numbers in Python is the simplest starting point.

The flag-toggle method’s O(n) run-time is the kind of trade-off that placement interviewers and code reviewers both surface, not to reject the answer but to see whether you can explain why it matters. If you want to take that kind of Python reasoning into applied AI exercises, TinkerLLM runs problem sets starting at ₹299.

Primary sources

Frequently asked questions

Does n & 1 work correctly for negative numbers in Python?

Yes. Python's bitwise AND uses two's complement representation. -5 & 1 equals 1 (odd) and -4 & 1 equals 0 (even), both correct results.

What is the time complexity of the flag toggle method?

O(n). The loop runs n times, making it impractical for large inputs. Use the bitwise AND or integer division method instead.

Why do interviewers ask for odd/even without the modulus operator?

It tests whether you understand bitwise operations and can express the same logic in multiple ways. The modulus is fine in real code; the constraint is a deliberate problem-solving exercise.

Does the integer division method handle negative numbers correctly?

Yes. Python floor-divides toward negative infinity: -5 // 2 equals -3, and -3 times 2 equals -6, which does not equal -5, so -5 is correctly identified as odd.

Which method should I use in actual Python code?

Use n % 2 for clarity. The bitwise AND method (n & 1) is the best alternative when avoiding the modulus operator is a stated constraint.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF