In this tutorial, we will write a java program to print half sandglass star patterns. Before that, you may go through the following topic in java.
The program below takes a user input for the number of rows needed and then in decreasing order of the first half is printed and then the increasing order of the second half of the half part of sandglass is printed.
This is a half hourglass pattern program in java or you can say half sandglass pattern program using a star.
Half Hourglass 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 35 36 37 38 | import java.util.Scanner; public class Main { public static void main(String[] args) { int rows, i, j, k; Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows: "); rows = sc.nextInt(); System.out.print("Output: \n\n"); //upper half for (i = 1; i <= rows; i++) { for (k = 1; k < i; k++) System.out.print(" "); for (j = i; j <= rows; j++) System.out.print("*"); System.out.println(); } //lower half for (i = rows - 1; i >= 1; i--) { for (k = 1; k < i; k++) System.out.print(" "); for (j = i; j <= rows; j++) System.out.print("*"); System.out.println(); } } } |
Output:
![java half hourglass pattern](https://simple2code.com/wp-content/uploads/2021/10/java-half-hourglass-pattern.jpg)