Google News
logo
Java Program to Print the following Pattern
Here's a Java program to print the following pattern :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Program :
public class Pattern {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}
Output :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
This program uses two nested for loops. The outer loop iterates over the rows of the pattern, and the inner loop iterates over the columns.

The inner loop prints the value of j followed by a space for each column, and the outer loop adds a newline character at the end of each row.