Hollow Inverted Pyramid Pattern in C Language with Free Codes

Hollow Inverted Pyramid

Unlike regular Inverted Pyramids where each row has fewer stars than the preceding one, Hollow Inverted Pyramid Patterns feature stars only at the edges of each row, and the inner spaces is blank. This illusion creates the Inverted Pyramid Pattern with Hollow spaces inside.

Output

Enter the number of rows: 5
*********
 *     *
  *   *
   * *
    *
Free Hand-Written Notes

Codes – Hollow Inverted Pyramid

Here is the codes of Hollow Inverted Pyramid Pattern,

#include <stdio.h>

int main() {
    int rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = rows; i >= 1; i--) {
        // Print leading spaces
        for (int j = 1; j <= rows - i; j++) {
            printf(" ");
        }

        // Print stars and spaces
        for (int j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1 || i == rows) {
                printf("*");
            } else {
                printf(" ");
            }
        }

        printf("\n");
    }

    return 0;
}

Codes Explanation

  1. Firstly, The program shows the prompts to the user to enter the number of rows for the hollow inverted pyramid.
  2. Then it will iterates through each row using a for loop in descending order.
  3. Next, Within each row, it prints leading spaces to align the pyramid correctly.
  4. Then it will determines the whether to print a star or space based on the position within the row and whether it’s the last row.
  5. After that, Stars are printed at the beginning and end of each row, leaving the inner spaces empty.
  6. Finally, it moves to the next line to start the next row.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top