Hollow Pyramid Pattern in C Language with Free Codes

Hollow Pyramid Pattern

A Hollow Pyramid Pattern resembles a solid pyramid but with the inner spaces left empty. This pattern is achieved by printing stars only at the edges of each row and leaving the inner space is blank. Each row of the pyramid consists of a combination of stars and spaces, strategically positioned to create the hollow effect.

Output

Enter the number of rows: 5
    *
   * *
  *   *
 *     *
*********
Free E-Books

Codes – Hollow Pyramid Pattern

Here is the codes of Hollow Pyramid Star Pattern,

#include <stdio.h>

int main() {
    int rows;

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

    for (int i = 1; i <= rows; 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. Here set the Firstly, The program prompts for the user to enter the number of rows for the hollow pyramid.
  2. The it will iterates through each row using a for loop.
  3. Within each row, it prints leading spaces to align the pyramid correctly.
  4. Then it determines whether to print a star or space based on the position within the row and whether it’s the last row.
  5. 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