Inverted Pyramid Pattern
Unlike regular pyramids where the each row expands when it descends, but the Inverted Pyramid Pattern start with a broad base and narrow down towards the top. This is achieved by gradually decreasing the number of stars in each row while maintaining the centered alignment. The number of rows determines the height of Pyramid.
Output
Enter the number of rows: 5
*********
*******
*****
***
*
Free E-Books
Codes – Inverted Pyramid Pattern
#include <stdio.h> int main() { int rows, space, star; printf("Enter the number of rows: "); scanf("%d", &rows); for (int i = rows; i >= 1; i--) { // Print spaces for (space = 0; space < rows - i; space++) { printf(" "); } // Print stars for (star = 1; star <= 2 * i - 1; star++) { printf("*"); } printf("\n"); } return 0; }
Codes Explanation
- Firstly, The program prompts to the user to enter the number of rows for making the inverted pyramid.
- Secondly, then it iterates through each row using a “
for
” loop in descending order. - Thirdly, this Within each row, it prints spaces (‘ ‘) to center-align the stars.
- Fourth, After printing the spaces, it prints the stars (
*
) using another “for
“ loop. - Fifth, the numbers of stars in each row follows a pattern: decreasing from the base to the top.
- Finally, it moves to the next line to start the next row.