Print Even Numbers in Java: Explanation & Practice

Print even numbers in a range

Problem summary

Print all even numbers from 2 to 10, each on a new line.

Starter code

public class Main {
    public static void main(String[] args) {
        // Print even numbers from 2 to 10
    }
}

Expected output and test cases

  • Even numbers 2-10
    2
    4
    6
    8
    10

Hints

  1. Start at 2 and increment by 2 each time
  2. Or use modulus to check if number is even
  3. i += 2 increments by 2

Related Control Flow exercises

Practice all Control Flow exercises · Run this idea in the Java compiler