Inverted Right-Angled Triangle Star Pattern in C++

What You'll Learn
This program prints an inverted right-angled triangle where each next row contains one less star than the previous row.
Row i prints exactly i stars while i decreases 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 <= i; ++j) cout << "*";
cout << "\n";
}
return 0;
}🧠 How It Works
Streams setup
#include <iostream> and cout replace C’s printf. rows sets how many characters appear on the first line.
Outer loop: wide row first
for (i = rows; i >= 1; --i) counts down so the first line has rows stars and each following line has one fewer.
Inner loop: cout << "*"
for (j = 1; j <= i; ++j) streams exactly i asterisks on the current row—same inner bound as the growing triangle, opposite outer direction.
Newline
cout << "\n" ends each row after the star run completes.
Inverted triangle
Total stars remain n(n + 1)/2 — O(rows²) time, O(1) extra space. The widest line scrolls 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 <= i; ++j) cout << "*";
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Use
cinso row count is chosen at runtime - Switch the outer loop to
1..rowsand printrows - i + 1stars for the same shape - Print numbers (
12345,1234, …) instead of* - Build a hollow inverted triangle (border stars only)
- Compare side-by-side with the standard right-angled triangle from Program 1
Avoid
- Forgetting
cout << "\n"orendlafter each row - Mixing up
i >= 1andi > 0without adjusting the inner bound - Using
cinwithout checking thatrowsis positive - Assuming the inner loop must always run
1..iwhen a formula could also work - Hard-coding
5when you meant to use therowsvariable
Key Takeaways
The outer loop runs from rows down to 1; the inner loop prints that many stars per row.
Row with i = k prints exactly k stars—so the first row is the widest.
Time complexity is still O(n²) for n rows; only the loop direction changed.
You can implement the same shape with a forward outer loop and (rows - i + 1) stars in the inner loop.
This pattern pairs naturally with the upright triangle—master both to read nested loops in either direction.
❓ Frequently Asked Questions
rows and decrements to 1. For each i, the inner loop prints i stars, so the first line is longest and each line shrinks by one—an upside-down right triangle.for (i = 1; i <= rows; ++i) (stars grow each row). This one uses for (i = rows; i >= 1; --i) (stars shrink each row).for (i = 1; i <= rows; ++i) and print (rows - i + 1) stars in the inner loop. Both approaches yield the same output.Next: Right-Aligned Triangle
Continue to Program 3 to print a right-aligned star triangle in C++.
This pattern is the direct mirror of Program 1. Once you master both, try right-aligned versions (Programs 3 and 4).
15 people found this page helpful
