Inverted Right-Aligned Triangle Star Pattern in C++

What You'll Learn
This program prints an inverted right-aligned triangle by printing increasing spaces and decreasing stars. The right edge stays aligned.
Row i prints rows - i spaces and i stars (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 <= i; ++j) cout << "*";
cout << "\n";
}
return 0;
}🧠 How It Works
Outer loop: i from rows down
for (i = rows; i >= 1; --i) prints the widest row first. As i shrinks, both the space prefix and star suffix adjust automatically.
Growing indent
for (j = 1; j <= rows - i; ++j) cout << " "; — when i is large, rows - i is small; as i drops, more leading spaces appear.
Shrinking star block
for (j = 1; j <= i; ++j) cout << "*"; reuses j for the star run. Star count tracks the current i.
Newline
cout << "\n" after both inner loops. Characters per row: (rows - i) + i = rows, so the right column stays aligned.
Inverted, still right-aligned
O(rows²) writes, O(1) extra space. Wide top rows use the tutorial’s horizontal scroll on phones.
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 <= i; ++j) cout << "*";
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate
rows > 0after input - Rewrite the star loop as
for (k = 1; k <= rows - i + 1; ++k)and compare readability - Switch to Program 3’s star loop
1..ito get the non-inverted right-aligned triangle - Print a right-aligned number ladder (54321, 5432, …)
- Combine with Program 2 mentally: both shrink stars per row; Program 4 adds spaces
Avoid
- Using
j <= iwhen you meantj < ifor spaces (extra space breaks alignment) - Using
kfrom1toihere—that is Program 3’s growing triangle, not this pattern - Forgetting the newline after each row
- Assuming
cinalways succeeds without validating input - Mixing tabs and spaces in the output
Key Takeaways
Row i: print i - 1 spaces, then rows - i + 1 stars.
The star loop k = i .. rows counts exactly those stars in one pass.
Program 3 grows stars; Program 4 shrinks them—same right column, different profile.
Time complexity is O(n²) for n rows.
Master Programs 1–4 and you have left, inverted, right-aligned, and inverted right-aligned triangles.
❓ Frequently Asked Questions
i - 1 spaces, then run k from i to rows for stars. Each row is shorter on the left side of the star block but still ends at the same column, so it looks inverted and right-aligned.rows - i + 1, which is exactly how many stars belong on row i. You can rewrite it as k from 1 to rows - i + 1 if you prefer.rows - i and stars 1..i. Program 4 uses spaces 1..i-1 and stars from i to rows. Same alignment idea; inverted star counts.n rows: each row does Θ(n) character prints.Next: Pyramid Pattern
Continue to Program 5 to print a centered pyramid star pattern in C++.
If you reverse the outer loop of Program 3, you get this inverted right-aligned triangle.
12 people found this page helpful
