Right-Aligned Triangle Star Pattern in C++

What You'll Learn
This program prints a right-aligned triangle by printing spaces first and then stars. Each next row has one fewer leading space and one more star.
Row i prints rows - i spaces and i stars.
⭐ 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 = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) cout << " ";
for (j = 1; j <= i; ++j) cout << "*";
cout << "\n";
}
return 0;
}🧠 How It Works
Outer row index
for (i = 1; i <= rows; ++i) walks from the one-star line up to the full-width base. The same j variable is reused for two consecutive inner loops.
Padding with spaces
for (j = 1; j <= rows - i; ++j) cout << " "; prints the left margin so stars line up on the right.
Star run
A second inner loop reuses j: for (j = 1; j <= i; ++j) cout << "*"; emits i stars after the padding.
Newline and fixed width
cout << "\n" ends the row. Each line prints (rows - i) + i = rows characters before the newline, so the right edge stays straight.
Right-aligned triangle
Star total is still n(n + 1)/2. O(rows²) output, O(1) extra space. Full-width rows scroll inside the green glyph on narrow viewports.
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 = 1; i <= rows; ++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 > 0aftercin - Remove the space loop to get the left-aligned triangle from Program 1
- Use a forward row loop with
(rows - i + 1)stars in one inner loop (same shape, different formulation) - Print digits instead of
*for a right-aligned number triangle - Try a hollow right-aligned triangle (border only)
Avoid
- Swapping
rows - iandibetween the two inner loops - Forgetting
cout << "\n"orendlafter each row - Using
j < rows - iwhen you meant<=(off-by-one spacing) - Mixing tab characters with spaces (alignment breaks in different terminals)
- Assuming
rowsis valid without checkingcin/ input validation
Key Takeaways
Right alignment = print (rows - i) spaces, then i stars, per row.
Two inner loops are used because spaces and stars have different counts each row.
Star counts per row match Program 1; only the leading padding changes.
Time complexity is O(n²) for n rows (quadratic total output).
This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.
❓ Frequently Asked Questions
i, print (rows - i) spaces, then i stars. The spaces push the stars to the right so the hypotenuse lines up on the right side of the triangle.i grows, while stars increase. One loop can only drive one of those counts cleanly.i per row). Program 3 adds a space loop first so the same i stars appear shifted right.n rows: each row prints Θ(n) characters, and there are n rows.Next: Inverted Right-Aligned Triangle
Continue to Program 4 to print the inverted right-aligned star triangle in C++.
If you reverse the outer loop (start from rows down to 1), you’ll get the inverted right-aligned triangle (Program 4).
14 people found this page helpful
