Check Even or Odd

Java coding interview problem for Basic Number Programs: Check Even or Odd.

Checking whether a number is Even or Odd is one of the simplest yet most frequently asked Java coding interview questions. It helps interviewers evaluate your understanding of arithmetic operators, conditional statements, and basic problem-solving skills.

Although simple, this concept is used in many advanced programming problems involving arrays, mathematics, bit manipulation, and algorithms.


What is an Even Number?

An Even Number is any integer that is completely divisible by 2.

Mathematically,

Number % 2 = 0

Examples

2

4

10

100

256

All of these are Even Numbers.


What is an Odd Number?

An Odd Number is any integer that is not completely divisible by 2.

Mathematically,

Number % 2 != 0

Examples

1

3

7

25

101

All of these are Odd Numbers.


Real Interview Question

Write a Java program to determine whether a given number is Even or Odd.


Example 1

Input

24

Output

24 is Even

Example 2

Input

35

Output

35 is Odd

Example 3

Input

0

Output

0 is Even

Example 4

Input

-17

Output

-17 is Odd

Negative numbers follow the same Even/Odd rules.


Understanding the Logic

The easiest way to determine whether a number is Even or Odd is by using the modulus (%) operator.

The modulus operator returns the remainder after division.

For Even Numbers

Number % 2 = 0

For Odd Numbers

Number % 2 = 1

or

Number % 2 = -1

(depending on the programming language and sign of the number)


Visual Representation

Input

18

Division

18 ÷ 2 = 9

Remainder = 0

Therefore

18 is Even

Input

19

Division

19 ÷ 2 = 9

Remainder = 1

Therefore

19 is Odd

Brute Force Approach

The simplest solution is

  • Divide the number by 2.
  • Check the remainder.
  • If the remainder is 0, the number is Even.
  • Otherwise, the number is Odd.

Algorithm

Step 1

Read the input number.

Step 2

Calculate

number % 2

Step 3

If the remainder is

0

Print

Even

Step 4

Otherwise

Print

Odd

Dry Run

Input

18

Calculation

18 % 2

=

0

Condition

18 % 2 == 0

Result

Even

Another Dry Run

Input

27

Calculation

27 % 2

=

1

Condition

27 % 2 == 0

Result

Odd

Dry Run with Negative Number

Input

-15

Calculation

-15 % 2

=

-1

Condition

-15 % 2 == 0

Result

Odd

Approach 1 — Using Modulus (%) Operator

This is the most common and interview-preferred solution.


Complete Java Program

public class EvenOrOdd {

    public static void main(String[] args) {

        int number = 25;

        if (number % 2 == 0) {

            System.out.println(number + " is Even");

        } else {

            System.out.println(number + " is Odd");

        }

    }

}

Output

25 is Odd

Step-by-Step Code Explanation

Step 1

Declare the input number.

int number = 25;

Current value

25

Step 2

Use the modulus operator.

number % 2

For

25

Calculation

25 % 2

=

1

The remainder is

1

Step 3

Check the condition.

if (number % 2 == 0)

If true

Even

Otherwise

Odd

Step 4

Print the result.

System.out.println(number + " is Odd");

Output

25 is Odd

Example Execution

Input

40

Processing

40 % 2

=

0

Output

40 is Even

Input

73

Processing

73 % 2

=

1

Output

73 is Odd

Why Does This Work?

The modulus operator returns the remainder after division.

When a number is divided by 2, there are only two possible outcomes.

Case 1

Remainder = 0

The number is

Even

Case 2

Remainder ≠ 0

The number is

Odd

Since every integer must fall into one of these two categories, this method always produces the correct result.


Advantages of This Approach

  • Very easy to understand.
  • Uses only one arithmetic operator.
  • Executes in constant time.
  • Works for both positive and negative integers.
  • Most common solution asked in Java interviews.
  • Highly readable and maintainable.

Drawbacks

Although this is the standard solution, interviewers often ask follow-up questions such as:

  • Can you solve it without using the modulus (%) operator?
  • Can you use bitwise operators?
  • Which approach is faster?
  • Why does the bitwise approach work?
  • Which approach would you use in production code?

we'll cover:

  • Bitwise (&) operator approach
  • Reusable method
  • Time and space complexity
  • Comparison of both approaches
  • Common interview mistakes
  • Frequently asked interview questions
  • Related coding problems
  • Key takeaways
  • Interview tips

Approach 2 — Using Bitwise AND (&) Operator

Another popular interview solution uses the bitwise AND (&) operator.

This approach avoids the modulus (%) operator and is considered slightly more efficient at the hardware level.


Understanding Bitwise AND

Every integer is stored in binary format.

For an Even Number, the last binary bit is always

0

For an Odd Number, the last binary bit is always

1

Examples

Decimal Binary Last Bit Type
8 1000 0 Even
10 1010 0 Even
15 1111 1 Odd
21 10101 1 Odd

Why Does number & 1 Work?

The binary value of

1

is

0001

Performing

number & 1

checks only the last bit.

If

Last Bit = 0

Output

0

The number is Even.

If

Last Bit = 1

Output

1

The number is Odd.


Example

Number

14

Binary

1110

Operation

1110

0001

-----

0000

Result

0

Therefore

14 is Even

Number

15

Binary

1111

Operation

1111

0001

-----

0001

Result

1

Therefore

15 is Odd

Java Program

public class EvenOddBitwise {

    public static void main(String[] args) {

        int number = 17;

        if ((number & 1) == 0) {

            System.out.println(number + " is Even");

        } else {

            System.out.println(number + " is Odd");

        }

    }

}

Output

17 is Odd

Approach 3 — Using a Reusable Method

Creating a reusable method improves

  • Readability
  • Code reuse
  • Unit testing
  • Maintainability

Java Program

public class EvenOddMethod {

    static boolean isEven(int number) {

        return number % 2 == 0;

    }

    public static void main(String[] args) {

        int number = 48;

        if (isEven(number)) {

            System.out.println(number + " is Even");

        } else {

            System.out.println(number + " is Odd");

        }

    }

}

Output

48 is Even

Time Complexity

Modulus Operator

Operation Complexity
Time O(1)
Space O(1)

Bitwise AND

Operation Complexity
Time O(1)
Space O(1)

Comparison of All Approaches

Approach Time Space Readable Recommended
Modulus (%) O(1) O(1) ⭐⭐⭐⭐⭐ ✅ Best for Interviews
Bitwise AND (&) O(1) O(1) ⭐⭐⭐⭐ Advanced Interview
Reusable Method O(1) O(1) ⭐⭐⭐⭐⭐ Production Ready

Common Mistakes

Mistake 1

Using

number / 2 == 0

Wrong

if(number / 2 == 0)

Division does not determine whether a number is even.

Always use

number % 2 == 0

Mistake 2

Confusing % with /

Wrong

number / 2

Correct

number % 2

The modulus operator returns the remainder.


Mistake 3

Incorrect Bitwise Condition

Wrong

(number & 2) == 0

Correct

(number & 1) == 0

Only the least significant bit determines whether a number is even or odd.


Mistake 4

Ignoring Negative Numbers

Example

-8

Output

Even

Example

-5

Output

Odd

Both % and & work correctly for negative integers.


Mistake 5

Using Complex Logic

Some beginners write

if(number % 2 == 1)

This fails for negative odd numbers because

-5 % 2 = -1

Instead, use

if(number % 2 == 0)

Otherwise,

the number is Odd.


Interview Follow-up Questions

Q1. Check Even or Odd without using %.

Q2. Explain why number & 1 works.

Q3. Print all Even Numbers from 1 to N.

Q4. Print all Odd Numbers from 1 to N.

Q5. Count Even and Odd numbers in an array.

Q6. Separate Even and Odd elements in an array.

Q7. Find the sum of Even numbers.

Q8. Find the sum of Odd numbers.

Q9. Determine whether a number is divisible by 4.

Q10. Compare % and & operators.


Related Coding Problems

  • Prime Number
  • Positive or Negative Number
  • Largest of Three Numbers
  • Leap Year
  • Reverse Integer
  • Count Digits
  • Sum of Digits
  • Fibonacci Series

Key Takeaways

  • An Even Number is divisible by 2.
  • An Odd Number is not divisible by 2.
  • The modulus (%) operator is the simplest and most commonly used solution.
  • The bitwise (&) operator checks the least significant bit and is a common interview optimization.
  • Both approaches run in O(1) time and use O(1) extra space.
  • For readability and maintainability, the modulus approach is generally preferred in production code.

Interview Tip

If an interviewer asks:

"Write a Java program to check whether a number is Even or Odd."

Start with the modulus (%) operator solution because it is the most readable and widely accepted. After solving it, mention the bitwise (&) operator approach and explain that every even number ends with 0 in binary while every odd number ends with 1. Demonstrating both solutions—and understanding why the bitwise approach works—shows a deeper understanding of Java fundamentals and binary arithmetic.