Number Patterns in Java

Print a simple increasing number triangle pattern.

BeginnerModule 3: Loop ProgramsExample 15 of 25
number-patterns.java
1public class Main {
2 public static void main(String[] args) {
3 int n = 5;
4 for (int i = 1; i <= n; i++) {
5 for (int j = 1; j <= i; j++) {
6 System.out.print(j + " ");
7 }
8 System.out.println();
9 }
10 }
11}

Output

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

What's going on

We use nested loops: outer for rows, inner for printing numbers up to the current row.