Inverted Hollow Pyramid using Stars in Java

star pattern 8

In this tutorial, we will write a program to print an inverted hollow pattern of stars in java. Before that, you may go through the following topic in java.

Both of the programs below take the user input for the number of rows and display the result using the for loop in java. The program is basically the inverted hollow triangle star pattern in java program.


Inverted hollow pyramid star pattern in java

import java.util.Scanner;

public class Main
{
  public static void main(String[] args)
  {
    int row, i, j;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter the number of rows: ");
    row = sc.nextInt();

    for (i = row; i > 0; i--)
    {
      for (j = 1; j <= row - i; j++)
        System.out.print(" ");
      if (i == 1 || i == row)
      {
        for (j = 1; j <= i *2 - 1; j++)
          System.out.print("*");
      }
      else
      {
        for (j = 1; j <= i *2 - 1; j++)
          if (j == 1 || j == i *2 - 1)
            System.out.print("*");
          else
            System.out.print(" ");
      }

      System.out.println();
    }
  }
}

Output:

Inverted hollow pyramid star pattern in java