Java print even numbers while loop

We try to solve specific logical problems in Java using some form of loops. In this post we present an example on how to print even numbers using while loop in Java

Java print even numbers while loop

Below is a simple example on how we can achieve the required logic. Below is a simple example on how to print even numbers using while loop in Java:
/**
 * An example program that print even numbers using while loop in Java.
 */
public class ExampleProgram {
    public static void main(String[] args) {
        int number = 2;
        while (number <= 100) {
            System.out.print(number);
            System.out.print(" ");
            number += 2;
        }
    }
}
The code above prints the number between 1 and 100 that are even. And doing so will give the output below
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100