How to write a C Program to Print Star Pyramid Pattern with an example? And also show you how to print Pyramid Pattern (or Equilateral Triangle) with different symbols.
C Program to Print Star Pyramid Pattern using While Loop
This program allows the user to enter the maximum number of rows the user wants to print. Here, we will print the Pyramid of * symbols until it reaches the user-specified rows.
#include <stdio.h> int main() { int Rows, i, j, k = 0; printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); printf("Printing Star Pyramid Pattern \n \n"); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= Rows-i; j++ ) { printf(" "); } while (k != (2 * i - 1)) { printf("*"); k++; } k = 0; printf("\n"); } return 0; }

Analysis
Let us see the Nested for
Outer Loop – First Iteration of C Program to Print Star Pyramid Pattern
From the above C Programming screenshot, you can observe that the value of i is 1 and Rows is 10, so the condition (i <= 10) is True. So, it will enter into the second for loop.
Inner For Loop – First Iteration
The j value is 1 and the condition (j <= 9) is True. So, it will start executing the statements inside the loop. So, it will start executing printf(” “) statement until the condition fails.
Inner While Loop – First Iteration
The k value is 0, and the condition k != 2*i – 1 (0 != 1) is True. So, it will start executing the statements inside it. So, it will start executing printf(“*”) statements until the condition fails.
Next, the iteration will start from the beginning until both the Inner and Outer loop conditions fail.
C Program to Print Star Pyramid Pattern using For Loop
In this C program, we just replaced the While Loop with the For Loop. I suggest you refer For Loop article to understand the logic.
#include <stdio.h> int main() { int Rows, i, j, k; printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= Rows-i; j++ ) { printf(" "); } for (k = 1; k <= (2 * i - 1); k++) { printf("*"); } printf("\n"); } return 0; }
Please Enter the Number of Rows: 12
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
This C program allows the user to enter the Symbol and the number of rows he/she wants to print. It means that instead of printing pre-defined stars, the user can enter their own character.
#include <stdio.h> int main() { int Rows, i, j, k = 0; char ch; printf("Please Enter any Symbol : "); scanf("%c", &ch); printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= Rows-i; j++ ) { printf(" "); } while (k != (2 * i - 1)) { printf("%c", ch); k++; } k = 0; printf("\n"); } return 0; }
Please Enter any Symbol : $
Please Enter the Number of Rows: 12
$
$$$
$$$$$
$$$$$$$
$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$