C Program to Print Hollow Star Pyramid - Source Code, Output & Execution Flow

C Program to Print Hollow Star Pyramid

This C program prints a hollow star pyramid by combining spaces, boundary checks, and nested loops.

#include <stdio.h>
int main() {
    int rows, i, j, space;
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    for (i = 1; i <= rows; i++) {
        for (space = 1; space <= rows - i; space++) printf(" ");
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1 || i == rows) printf("*");
            else printf(" ");
        }
        printf("
");
    }
    return 0;
}
Enter number of rows: 5
    *
   * *
  *   *
 *     *
*********

In this program, the input is read first when required, the core logic is applied step by step, and the final result is displayed using printf(). Follow the execution flow beside the code to understand how each statement contributes to the output.

Sai Kishore Randhi, author
About the author

Sai Kishore Randhi

Embedded systems, VLSI, and chip design

Sai Kishore writes about embedded systems, VLSI, chip design, and the programming concepts behind modern hardware.

View LinkedIn profile ↗