C Program – Print Simple Pyramid Pattern - Star Pyramid

Print Simple Pyramid Pattern

A pyramid pattern displays rows of stars centred with respect to the base. Each row contains one more star than the previous, creating a triangular shape. To print the pyramid, we use two inner loops: one for spaces and one for stars.

#include <stdio.h>
int main() {
    int rows;
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    for (int i = 1; i <= rows; i++) {
        // Print spaces before stars
        for (int j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        // Print stars for the current row
        for (int k = 1; k <= 2 * i - 1; k++) {
            printf("*");
        }
        printf("\n");             // Move to the next line after each row
    }
    return 0;
}
Enter number of rows: 4
    *
  ***
  *****
*******
        

The nested loops manage the spacing and star counts for each row. Before printing stars, the program prints rows - i spaces so that the stars are centered. Then it prints 2*i - 1 stars, which increases by two for each row, giving the pyramid its shape.

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 ↗