Print Even Numbers
Print all even numbers in a given range.
BeginnerTopic: Module 3: Loop Programs
Java Print Even Numbers Program
This program helps you to learn the fundamental structure and syntax of Java programming.
public class Main {
public static void main(String[] args) {
for (int i = 2; i <= 100; i += 2) {
System.out.print(i + " ");
}
}
}Output
2 4 6 ... 100
Understanding Print Even Numbers
We start from 2 and increment by 2 to hit only even numbers.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.