Hollow pyramid star pattern in java

ht‮pt‬s://www.lautturi.com
Hollow pyramid star pattern in java

To print a hollow pyramid star pattern in Java, you can use a nested loop structure.

Here's an example of how to do it:

public class Main {
  public static void main(String[] args) {
    int n = 5; // number of rows

    // outer loop to print rows
    for (int i = 1; i <= n; i++) {
      // inner loop to print spaces
      for (int j = 1; j <= n - i; j++) {
        System.out.print(" ");
      }
      // inner loop to print stars
      for (int j = 1; j <= 2 * i - 1; j++) {
        // print stars for first and last rows, and for first and last columns
        if (i == 1 || i == n || j == 1 || j == 2 * i - 1) {
          System.out.print("*");
        } else {
          System.out.print(" ");
        }
      }
      // move to next line
      System.out.println();
    }
  }
}

This code will output the following pyramid pattern:

.   *
   * *
  *   *
 *     *
*********
Created Time:2017-11-01 12:04:58  Author:lautturi