Inverted 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 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:

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 = 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

1

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.

Outer
2

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.

Align
3

Shrinking star block

for (j = 1; j <= i; ++j) cout << "*"; reuses j for the star run. Star count tracks the current i.

Stars
4

Newline

cout << "\n" after both inner loops. Characters per row: (rows - i) + i = rows, so the right column stays aligned.

Width
=

Inverted, still right-aligned

O(rows²) writes, O(1) extra space. Wide top rows use the tutorial’s horizontal scroll on phones.

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 = 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 > 0 after 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..i to 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 <= i when you meant j < i for spaces (extra space breaks alignment)
  • Using k from 1 to i here—that is Program 3’s growing triangle, not this pattern
  • Forgetting the newline after each row
  • Assuming cin always succeeds without validating input
  • Mixing tabs and spaces in the output

Key Takeaways

1

Row i: print i - 1 spaces, then rows - i + 1 stars.

2

The star loop k = i .. rows counts exactly those stars in one pass.

3

Program 3 grows stars; Program 4 shrinks them—same right column, different profile.

4

Time complexity is O(n²) for n rows.

5

Master Programs 1–4 and you have left, inverted, right-aligned, and inverted right-aligned triangles.

❓ Frequently Asked Questions

Print 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.
That range has length 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.
Program 3 uses spaces 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.
O(n²) for n rows: each row does Θ(n) character prints.

Next: Pyramid Pattern

Continue to Program 5 to print a centered pyramid star pattern in C++.

Program 5 →
Did you know?

If you reverse the outer loop of Program 3, you get this inverted right-aligned triangle.

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.

12 people found this page helpful