Right-Aligned Triangle Star Pattern in C++

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

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:

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 <= rows - i; ++j) cout << " ";
        for (j = 1; j <= i; ++j) cout << "*";
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

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.

Outer
2

Padding with spaces

for (j = 1; j <= rows - i; ++j) cout << " "; prints the left margin so stars line up on the right.

Align
3

Star run

A second inner loop reuses j: for (j = 1; j <= i; ++j) cout << "*"; emits i stars after the padding.

Stars
4

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.

Width
=

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.

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 <= rows - i; ++j) cout << " ";
        for (j = 1; j <= i; ++j) cout << "*";
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate rows > 0 after cin
  • 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 - i and i between the two inner loops
  • Forgetting cout << "\n" or endl after each row
  • Using j < rows - i when you meant <= (off-by-one spacing)
  • Mixing tab characters with spaces (alignment breaks in different terminals)
  • Assuming rows is valid without checking cin / input validation

Key Takeaways

1

Right alignment = print (rows - i) spaces, then i stars, per row.

2

Two inner loops are used because spaces and stars have different counts each row.

3

Star counts per row match Program 1; only the leading padding changes.

4

Time complexity is O(n²) for n rows (quadratic total output).

5

This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.

❓ Frequently Asked Questions

For row 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.
You need separate loops (or equivalent logic) for two different repeat counts: spaces decrease as i grows, while stars increase. One loop can only drive one of those counts cleanly.
Program 1 prints only stars (i per row). Program 3 adds a space loop first so the same i stars appear shifted right.
O(n²) for 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++.

Program 4 →
Did you know?

If you reverse the outer loop (start from rows down to 1), you’ll get the inverted right-aligned triangle (Program 4).

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.

14 people found this page helpful