Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Print Even Numbers in Java

List even numbers in Java by starting a for loop at 2 and stepping by 2. The one-line output contains fifty values through 100, deliberately leaving out zero.

BeginnerJava Loop ProgramsExample 2 of 25
print-even-numbers.java
1public class Main {
2 public static void main(String[] args) {
3 for (int i = 2; i <= 100; i += 2) {
4 System.out.print(i + " ");
5 }
6 }
7}

Sample Output

2 4 6 ... 100

Each step adds two: 2 → 4 → 6, continuing to 100. The next value, 102, fails the condition.

In this one

  • Choose a useful starting value
  • Advance a counter by two
  • Avoid an unnecessary inner check

How it works

  1. 1

    Start i at 2.

  2. 2

    Check i <= 100.

  3. 3

    Print i and a separating space.

  4. 4

    Add 2 to reach the next even number.

Edge cases

Starting at zero

Change i to 0

Zero is included and the sequence contains 51 values through 100.

Odd upper limit

Use a limit of 99

The final printed even number is 98.

What's going on

Print the even numbers from 2 through 100. An even number divides into pairs without anything left over. Instead of checking every number, this program starts with an even value and moves ahead by two each time.

The for loop sets i to 2, checks i <= 100, and prints i followed by a space. The update i += 2 means add two to the current value. Adding two to an even number gives the next even number, so no separate if condition is needed inside the loop.

The output starts 2, 4, 6 and ends at 100. There are fifty printed values. Starting at 1 with the same update would print odd numbers instead. Zero is mathematically even, but it is not included because this particular range starts at 2. Keep the starting value and the requested range separate in your thinking.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does i += 2 do?

Pick an answer to check it.

Question 2

What follows 98?

Pick an answer to check it.

Questions people ask

Why is no % check used?

Starting at 2 and adding 2 guarantees each value is even.

How many numbers print?

50, from 2 through 100 inclusive.

Is zero odd because it is missing?

No. Zero is even; it is outside the chosen range.