Print Even Numbers in Java
Print all even numbers in a given range.
BeginnerModule 3: 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}
Output
2 4 6 ... 100
What's going on
We start from 2 and increment by 2 to hit only even numbers.