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

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

The first inner loop prints spaces to align the * characters correctly, and the second inner loop prints * characters for each column.

The outer loop adds a newline character at the end of each row.