Print Numbers 1-5 in Java: Explanation & Practice

Use a for loop to print numbers

Problem summary

Print numbers from 1 to 5, each on a new line.

Starter code

public class Main {
    public static void main(String[] args) {
        // Use a for loop to print 1 through 5
    }
}

Expected output and test cases

  • Print 1 to 5
    1
    2
    3
    4
    5

Hints

  1. For loop syntax: for (init; condition; increment)
  2. Start at 1 and go while i <= 5
  3. Use i++ to increment by 1 each iteration

Related Control Flow exercises

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