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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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: