Inverted Pyramid Star Pattern in C++

What You'll Learn
This program prints an inverted pyramid by decreasing the odd number of stars row by row, while increasing leading spaces to keep the pyramid centered.
Star count per row is 2 * i - 1 (with i decreasing from rows to 1).
⭐ Pattern Output
When you run the program with rows = 5:
*********
*******
*****
***
*Complete C++ Program
Fixed rows = 5 version:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= rows - i; ++j) cout << " ";
for (j = 1; j <= 2 * i - 1; ++j) cout << "*";
cout << "\n";
}
return 0;
}🧠 How It Works
Outer loop: base to tip
for (i = rows; i >= 1; --i) prints the wide row first. Inner logic matches the upright pyramid; only the outer direction flips.
Leading spaces
for (j = 1; j <= rows - i; ++j) cout << " "; — as i shrinks, rows - i grows, indenting narrower rows more.
Odd stars, shrinking
for (j = 1; j <= 2 * i - 1; ++j) cout << "*"; removes two stars per row as i steps down.
Newline
cout << "\n" after each pair of inner loops completes the centered line.
Inverted centered pyramid
O(rows²) character writes, O(1) extra space. Long middle rows scroll sideways in the mobile-friendly result strip.
Variation — User Input Version
Accept rows with cin:
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= rows - i; ++j) cout << " ";
for (j = 1; j <= 2 * i - 1; ++j) cout << "*";
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Flip back to Program 5 by changing the outer loop to
1..rows - Validate
rows > 0aftercin - 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
cout << "\n"orendlbetween rows - Ignoring invalid input or negative
rowsaftercin
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.Next: Inverted V Hollow Pattern
Continue to Program 7 to print an inverted V-shaped hollow star pattern in C++.
If you print an upright pyramid (Program 5) followed by this inverted pyramid, you get the filled diamond shape (Program 10).
11 people found this page helpful
