Inverted Hollow Pyramid Star Pattern

Program to Print Inverted Hollow Pyramid Star Pattern

PRINTING PATTERN:

*******
     *   *
      * *
       *
            

PREREQUISITE:

Basic knowledge of Java language and loops.

ALGORITHM:

1. Take the number of rows as input from the user (height of the inverted hollow pyramid) and store it in a variable ('n').
2. Run a loop 'n' times to iterate through the rows (i=0 to i<n).
3. For the first row, print stars for the entire row.
4. For the rows in between, print spaces for the hollow part, and stars at the beginning and end.
5. For the last row, print a single star.
6. Move to the next line after each row has been printed.

Code in Java:

import java.util.Scanner;

public class InvertedHollowPyramidPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of rows for the inverted hollow pyramid:");
        int n = scanner.nextInt();  // Taking input for number of rows

        for (int i = 0; i < n; i++) {
            // Print spaces for inverted hollow pyramid shape
            for (int j = 0; j < i; j++) {
                System.out.print(" ");  // Printing spaces
            }
            // Print stars for each row
            for (int j = 0; j < (2 * (n - i) - 1); j++) {
                // Print '*' for the first row, and first and last column for hollow rows
                if (i == 0 || j == 0 || j == (2 * (n - i) - 1)) {
                    System.out.print("*");  // Printing stars
                } else {
                    System.out.print(" ");  // Printing spaces for hollow part
                }
            }
            System.out.println();  // New line after each row
        }

        scanner.close();
    }
}
            

Output:

Enter the number of rows for the inverted hollow pyramid:
4
*******
     *   *
      * *
       *