- Write a program in C to print pyramid star pattern using for loop.
- Write a C program to print an equilateral triangle star pattern of n rows using loops.
For an equilateral triangle pyramid star pattern of 5 rows. Program’s output should be:

Required Knowledge
- For Loop
- printf and scanf function in C
Algorithm to print pyramid star pattern using loop
In this program, we are printing a pyramid star pattern in which ith row contains (2*i + 1) space separated stars. The index of rows and columns starts from 0.
- We first take the number of rows(R) in the pattern as input from user using scanf function.
- One iteration of outer for loop will print a row of pyramid(from i = 0 to R – 1).
- For jth row of pyramid, the inner for loop first prints (R – i – 1) spaces for every line and then nested while loop prints (2*j + 1) stars.
Here is the matrix representation of the pyramid star pattern. The row numbers are represented by i whereas column numbers are represented by j.

C program to print pyramid star pattern

#include<stdio.h>
int main() {
int i, j, rows, star = 0;
printf("Enter the number of rows\n");
scanf("%d", &rows);
/* printing one row in every iteration */
for (i = 0; i < rows; i++) {
/* Printing spaces */
for (j = 0; j <= (rows - i - 1); j++) {
printf(" ");
}
/* Printing stars */
while (star != (2 * i + 1)) {
printf("*");
star++;;
}
star = 0;
/* move to next row */
printf("\n");
}
return 0;
}
Output
Enter the number of rows
6
*
***
*****
*******
*********
***********