How to print even numbers in Java using while loop

In this post we try to give example on how to print even numbers in java using while loop. This is pretty simple so head down to the sample code:

How to print even numbers in Java using while loop

As an example, we show below how to print the first 20 even numbers on the console using while loop. This is a single loop example, and no need for nested loop:
/**
 * An example program that prints the first 20 even numbers.
 */
public class ExampleProgram {
    public static void main(String[] args) {
        int i = 0;
        while (i <= 10) {
            System.out.println(i * 2);
            i++;
        }
    }
}

So below is the output of the code above when we try to run it:

0
2
4
6
8
10
12
14
16
18
20