- 1C Program - Print Simple Pyramid Pattern
- 2C Program - Print Given Triangle
- 3C Program - Print 180° Rotation of Simple Pyramid
- 4C Program - Print Inverted Pyramid
- 5C Program - Print Number Pattern
- 6C Character Pattern Program
- 7C Hollow Star Pyramid Pattern
- 8C Floyd's Triangle Program
- 9C Pascal's Triangle Program
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.
⬅ Previous TopicC Program - Display Armstrong Number Between Two Intervals
Next Topic ⮕C Program - Print Given Triangle
Next Topic ⮕C Program - Print Given Triangle
Comments
Loading comments...