Right-Angled Triangle Star Pattern in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n rows total

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:

Output
*
**
***
****
*****
1

Complete C++ Program

Fixed rows = 5 version:

C++
#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

1

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.

Setup
2

Outer loop: one row per i

for (i = 1; i <= rows; ++i) advances the row index. Row i will contain exactly i stars.

Outer
3

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.

Inner
4

End the row

cout << "\n" (or endl if you prefer a flush) finishes the row before the next outer iteration.

Newline
=

Right triangle

Total * count is 1 + 2 + … + n = n(n + 1)/2O(rows²) character writes, O(1) extra space. Long rows scroll horizontally inside the green preview on small screens.

2

Variation — User Input Version

Accept rows with cin:

C++
#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" or endl between rows
  • Using while loops when for is clearer
  • Skipping input validation with cin
  • Confusing ++i and i++ in loop context

Key Takeaways

1

The outer loop controls the number of rows, and the inner loop prints stars equal to the current row number.

2

Row i always contains exactly i stars, producing the classic right-triangle shape.

3

Time complexity is O(n²) — total operations = n(n+1)/2.

4

This pattern is the foundation for all other star patterns: pyramid, diamond, hollow shapes, and more.

5

Use cin to make the row count dynamic instead of hard-coding it.

❓ Frequently Asked Questions

The outer loop runs from 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.
Nested loops give two-dimensional control: the outer loop handles which row we’re on, and the inner loop handles how many stars to print on that row. This two-level control is required for any 2D pattern.
Yes. Reverse the outer loop: 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.
The time complexity is O(n²) where n is the number of rows. The inner loop prints 1+2+3+…+n = n(n+1)/2 total stars, which simplifies to O(n²).

Next: Inverted Right-Angled Triangle

Continue to Program 2 to print the inverted right-angled triangle pattern in C++.

Program 2 →
Did you know?

Once you’re comfortable with this pattern, try printing it inverted (Program 2) and then try centered pyramids (Programs 5 and 6).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful