In this tutorial, we will write a program to print a 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 hollow triangle star pattern in java program.
Hollow Pyramid using Stars 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 35 36 37 38 39 40 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows: "); int n = sc.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } if (i == 1 || i == n) { for (int j = 1; j <= i *2 - 1; j++) { System.out.print("*"); } } else { for (int 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: