In this tutorial, we will write a java program to print the inverted hourglass pattern using numbers. Before that, you may go through the following topic in java.
Inverted Hourglass 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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(); int nsp = 2 *n - 1; int nr = 2 *n + 1; int num = n; int a = 0; for (int i = 1; i <= nr; i++) { if (i > n + 1) a = nr - i + 1; else a = i; //numbers for (int cst = 1; cst <= a; cst++) { System.out.print(num); num--; } //Spaces for (int csp = 1; csp <= nsp; csp++) { System.out.print(" "); } for (int cst = 1; cst <= a; cst++) { num++; if (num != 0) System.out.print(num); } if (i <= (nr) / 2) nsp -= 2; else nsp += 2; System.out.println(); } } } |
Output: