#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for (int i = rows; i >= 1; i--) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
// Print stars
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}Output
Enter number of rows: 5
*********
*******
*****
***
*Inverted Pyramid in C++
This program teaches you how to print an inverted pyramid pattern using nested loops in C++. An inverted pyramid is the reverse of a full pyramid — it starts with the largest row at the top and decreases to a single star at the bottom. This pattern helps beginners understand reverse iteration and how to modify loop conditions to create different patterns.
What is an Inverted Pyramid?
An inverted pyramid is a pattern that looks like this (for 5 rows):
## ## *
## ***
## *
***
*
Notice:
- Starts with the maximum number of stars at the top
- Each row has fewer stars than the row above
- Stars are centered (spaces on the left)
- Forms an upside-down triangle
Understanding Reverse Iteration
The key difference from a full pyramid is the ## outer loop direction:
-
Full pyramid:
for (int i = 1; i <= rows; i++)→ counts up (1, 2, 3, 4, 5) -
Inverted pyramid:
for (int i = rows; i >= 1; i--)→ counts down (5, 4, 3, 2, 1)
This reverse iteration makes the pattern start large and get smaller.
Pattern Formulas
The formulas are the same as a full pyramid, but applied in reverse:
-
Spaces:
rows - ispaces on the left -
Stars:
2 * i - 1stars in each row
Why it works in reverse:
- When
i = rows(top row): Maximum stars, minimum spaces - When
i = 1(bottom row): Minimum stars, maximum spaces
Summary
- The outer loop counts down from
rowsto 1 usingi--. - The first inner loop prints
(rows - i)spaces (increases as we go down). - The second inner loop prints
(2 * i - 1)stars (decreases as we go down). - After each row,
endlmoves to the next line. - This creates an inverted pyramid that starts large at the top and shrinks to a point at the bottom.
This program is essential for understanding reverse iteration and how to create decreasing patterns. Combined with a full pyramid, you can create more complex patterns like diamonds.