Right-Angled Triangle Star Pattern in C++

What You'll Learn
This program prints a right-angled triangle where each next row contains one more star than the previous row.
Row i prints exactly 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 <= i; ++j) cout << "*";
cout << "\n";
}
return 0;
}🧠 How It Works
iostream and cout
#include <iostream> with using namespace std; exposes cout for character output. int rows = 5; fixes height in the demo; the variation uses cin instead.
Outer loop: one row per i
for (i = 1; i <= rows; ++i) advances the row index. Row i will contain exactly i stars.
Inner loop: stream stars
for (j = 1; j <= i; ++j) cout << "*"; appends each * to the same line. cout does not break the line until you send a newline.
End the row
cout << "\n" (or endl if you prefer a flush) finishes the row before the next outer iteration.
Right triangle
Total * count is 1 + 2 + … + n = n(n + 1)/2 — O(rows²) character writes, O(1) extra space. Long rows scroll horizontally inside the green preview on small screens.
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 <= i; ++j) cout << "*";
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Accept row count from user with
cin - Print an inverted triangle by reversing the outer loop
- Replace
*with numbers for a number triangle - Add spaces between stars for visual variety
- Create a hollow triangle (border stars only)
Avoid
- Hard-coding the row count in production code
- Forgetting
cout << "\n"orendlbetween rows - Using
whileloops whenforis clearer - Skipping input validation with
cin - Confusing
++iandi++in loop context
Key Takeaways
The outer loop controls the number of rows, and the inner loop prints stars equal to the current row number.
Row i always contains exactly i stars, producing the classic right-triangle shape.
Time complexity is O(n²) — total operations = n(n+1)/2.
This pattern is the foundation for all other star patterns: pyramid, diamond, hollow shapes, and more.
Use cin to make the row count dynamic instead of hard-coding it.
❓ Frequently Asked Questions
i = 1 to rows. For each row i, the inner loop runs from j = 1 to i, printing one star per iteration. So row 1 gets 1 star, row 2 gets 2 stars, and so on—forming the right-angled triangle.for (i = 5; i >= 1; i--). This starts with 5 stars in the first row and decreases by one each row, producing an upside-down right-angled triangle.Next: Inverted Right-Angled Triangle
Continue to Program 2 to print the inverted right-angled triangle pattern in C++.
Once you’re comfortable with this pattern, try printing it inverted (Program 2) and then try centered pyramids (Programs 5 and 6).
16 people found this page helpful
