Inverted Center-Aligned Pyramid Star Pattern in C

What You'll Learn
How to print an upside-down centered pyramid in C. The inner loops match Program 5: (rows - i) spaces, then (2 * i - 1) stars. The only structural change is the outer loop: i runs from rows down to 1, so the widest row prints first.
With rows = 5, star counts per printed line are 9, 7, 5, 3, 1.
⭐ Pattern Output
When you run the program with rows = 5, you’ll see:
*********
*******
*****
***
*Complete C Program
Complete C program for the inverted centered pyramid:
#include <stdio.h>
int main() {
int rows = 5;
int i, j, k;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= 2 * i - 1; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}🧠 How It Works
Declare Variables
rows is pyramid height. i is the current row index (counting down); j and k are space and star counters.
Outer Loop (Rows, Reverse)
for (i = rows; i >= 1; --i) prints the wide row first and the single-star row last.
Inner Loop — Spaces
for (j = 1; j <= rows - i; ++j) still prints rows - i spaces. As i drops, that count rises, indenting narrower rows.
Inner Loop — Stars
for (k = 1; k <= 2 * i - 1; ++k) prints an odd number of stars that shrinks as i shrinks.
New Line
printf("\n") ends each row. Inner loops match the upright pyramid; only for (i = rows; i >= 1; --i) reverses row order so the widest line prints first.
Inverted pyramid
Same per-row length formula as the centered pyramid: (rows - i) + (2i - 1). Total time O(rows²), extra space O(1). Long lines use the same touch-scrollable glyph strip on mobile.
Variation — User Input Version
Read rows with scanf():
#include <stdio.h>
int main() {
int rows;
int i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= 2 * i - 1; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Flip back to Program 5 by changing the outer loop to
1..rows - Validate
rows > 0afterscanf() - Draw a hollow inverted pyramid (outline only)
- Use digits instead of
*for an inverted number pyramid - Compare mentally with Program 2 (inverted left triangle without centering)
Avoid
- Changing the star bound to
2 * i(even widths break the usual symmetric look) - Using
i++on the outer loop by mistake (you need to decrement) - Altering the space formula without checking alignment
- Omitting
printf("\n")between rows - Ignoring
scanferrors or negativerows
Key Takeaways
Same inner loops as Program 5; reverse the outer loop to invert the pyramid.
As i decreases, rows - i grows and 2i - 1 shrinks—wider margin, fewer stars.
Printed star counts for rows = 5: 9, 7, 5, 3, 1.
Time complexity O(n²) for n rows.
This is the centered analogue of “invert the outer loop” from Program 1 → Program 2.
❓ Frequently Asked Questions
for (i = rows; i >= 1; --i), the first iteration uses the largest i, so you print the longest line first. Later iterations use smaller i, so 2*i-1 falls while rows-i rises.rows - i grows when i gets smaller. That extra left padding keeps shorter star runs centered under the first (widest) row.i from 1 to rows (tip to base). Program 6 decrements i from rows to 1 (base to tip). Inner loops are unchanged.n rows: same per-row print totals as Program 5, only iteration order differs.Explore More C Star Patterns!
Diamonds and hollow shapes combine upright and inverted halves you already know.
Program 6 is exactly Program 5 with the outer loop reversed—the same relationship as Program 1 versus Program 2, but with centered odd-width rows instead of a left-aligned wedge.
12 people found this page helpful
