Write a C program to print a pyramid star pattern
Description: Print a pyramid of stars with 4 rows.
Hint:
- In row
iprintrows - ispaces first, and then2 * i - 1stars.
C program to print a pyramid pattern
#include <stdio.h>
int main(void) {
int rows = 4;
for (int i = 1; i <= rows; i++) {
for (int s = 1; s <= rows - i; s++) {
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output:
Output of the above program is
*
***
*****
*******
How it works
- The first inner loop prints the spaces on the left, so the pyramid is in the centre.
- The second inner loop prints the stars: 1, 3, 5, 7 in the four rows.
Read C Nested Loops.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |