Check Even Number in Java

What you'll learn

  • How n % 2 == 0 checks parity in Java.
  • How to write reusable methods for one value and for ranges.
  • Common interview points like zero and negative numbers.

Prerequisites

Java operators, methods, loops, and integer types.

  • Understand that an even integer is divisible by 2.
  • Know Java modulo syntax: n % 2.

Math definition

An integer n is even when n = 2k for some integer k. Equivalent test: n % 2 == 0.

Intuition

10Even
Reason
10 % 2 == 0
11Odd
Reason
11 % 2 == 1

Live preview

Type an integer to test parity using the same rule as Java code examples.

Live result
Press "Check parity".

Algorithm

Goal: return true if n is divisible by 2.

Compute n % 2

If remainder is 0, the number is even; otherwise odd.

Reuse for ranges

For each i in [start, end], apply the same check and print matches.

📜 Pseudocode

Pseudocode
function isEven(n):
    return (n mod 2) == 0

function printEvensInRange(start, end):
    for i from start to end:
        if isEven(i):
            output i
1

Check a single number

java
public class Main {
    static boolean isEven(int number) {
        return number % 2 == 0;
    }

    public static void main(String[] args) {
        int number = 10;

        if (isEven(number)) {
            System.out.println(number + " is an even number.");
        } else {
            System.out.println(number + " is not an even number.");
        }
    }
}
📤 Output
10 is an even number.
2

Print even numbers in range

java
public class Main {
    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static void printEvenNumbers(int start, int end) {
        System.out.println("Even numbers in the range " + start + " to " + end + ":");
        for (int i = start; i <= end; i++) {
            if (isEven(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        printEvenNumbers(1, 10);
    }
}
📤 Output
Even numbers in the range 1 to 10:
2 4 6 8 10

Optimization

Step by two: align to first even and increment by 2 when printing ranges.

Bitwise alternative: (n & 1) == 0 also checks parity for Java integers.

🔄 Input / output examples

InputOutput meaning
0Even
10Even
11Odd
-4Even

Edge cases and pitfalls

Zero

n = 0

Zero is even because it is divisible by two.

Negative

n < 0

Negative values can still be even, such as -4.

Range

Bounds

Use inclusive loops carefully to avoid missing the end value.

⏱️ Time and space complexity

OperationTimeExtra space
isEven(n)O(1)O(1)
Scan [start, end]O(end - start + 1)O(1)

Summary

  • Use n % 2 == 0 to test even numbers in Java.
  • Reuse the same predicate for single values and full ranges.
  • Remember that 0 and negative multiples of 2 are even.

❓ FAQ

An integer n is even if n can be written as 2*k for some integer k.
Yes. 0 % 2 is 0, so Java treats 0 as even.
Yes. (n & 1) == 0 is another valid parity check for int and long.
Did you know?

Zero is even because 0 = 2 * 0. So n % 2 == 0 is true for n = 0.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful