C++ Inverted Triangle Star Pattern (Right-Angled)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An inverted right-angled triangle prints a left-aligned staircase of * characters that shrinks: the first row has rows stars, the last has one.

Remember
Rule: for i from rows down to 1, print i stars

*****
****
***
**
*     ← 5 rows (widest first)

It is the flip of Program 1: keep the same inner loop (j from 1 to i), reverse only the outer loop so the widest line prints first.

How to Solve It

Two ways to emit the same shape — start with a countdown outer loop, then optionally use a forward loop with a decreasing star count.

MethodIdeaBest for
Countdown outer loopi from rows down to 1; inner prints i starsLearning, interviews, exams
Forward + rows - i + 1Ascending i; star count shrinks each rowWhen you must keep i++

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from 1 to i:
        print "*" (no newline)
    print newline

Cheat sheet

GoalPattern
Countdown rowsfor (i = rows; i >= 1; --i)
Print i starsfor (j = 1; j <= i; ++j) cout << "*";
End the rowcout << "\n";
Forward alternatefor (i = 1; i <= rows; ++i) print rows - i + 1 stars
One-line shortcutcout << string(i, '*') << "\n"; while counting down
Upright versionfor (i = 1; i <= rows; ++i) → Program 1

Printing Stars vs Starting a New Line

APIEffectUse for
cout << "*"Stays on the same lineEach *
cout << "\n"Ends the current lineAfter the inner loop

Live Preview

Change the row count and the inverted triangle updates instantly — including the triangular star total.

Whole numbers from 1 to 20. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 stars
*****
****
***
**
*

Worked Walkthrough — rows = 4

Trace each outer-loop value of i as it counts down, and count how many times the inner loop runs.

iInner jPrinted rowStars
41..4****4
31..3***3
21..2**2
11..1*1

Total star prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same total as the upright triangle of height 4. That triangular sum is why time is O(n²).

C++ Programs

Three complete programs: countdown outer loop, console input, and a forward-loop alternate. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — classic reverse outer loop, the clearest invert of Program 1.

C++
#include <iostream>
using namespace std;

int main() {
    int rows = 5;

    for (int i = rows; i >= 1; --i) {
        for (int j = 1; j <= i; ++j) {
            cout << "*";
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Set height. rows = 5 means five lines from base to tip.

2. Outer loop counts down. i runs from rows down to 1 — widest row first.

3. Inner loop prints stars. For each i, j runs from 1 to i, so row i gets exactly i stars via cout << "*".

4. Break the line. cout << "\n" after the inner loop starts the next (shorter) row.

When i = 5 you get five stars; when i = 1 you get a single star.

Example 2 — User Input Version

Read the height at runtime. Check cin.fail() in real apps (shown in the tip below).

C++
#include <iostream>
using namespace std;

int main() {
    int rows;

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (int i = rows; i >= 1; --i) {
        for (int j = 1; j <= i; ++j) {
            cout << "*";
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for a row count, then store it with cin >> rows.

2. Same countdown core. Only the source of rows changes — the print logic matches Example 1.

3. Safer input tip. Bad input sets cin’s fail bit. Prefer:

Safer input
if (!(cin >> rows) || rows < 1) {
    cout << "Enter a positive whole number.\n";
    return 1;
}

Example 3 — Forward Loop + rows - i + 1

Ascending i with a decreasing star count — same shape without counting the outer loop backward.

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    int rows = 5;

    for (int i = 1; i <= rows; ++i) {
        int stars = rows - i + 1;
        cout << string(stars, '*') << "\n";
    }

    return 0;
}

How It Works

1. Ascending outer loop. i still runs from 1 to rows — same direction as Program 1.

2. Shrinking star count. stars = rows - i + 1 so when i = 1 you get 5 stars, when i = 5 you get 1.

3. One-line row. string(stars, '*') builds the whole row; "\n" ends it.

Learn the countdown version first (Examples 1–2); use this when an interviewer asks for an ascending outer loop.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Upright triangle by mistake

If you increment i from 1 to rows without changing the star bound, you reprint Program 1. Use i-- or print rows - i + 1 stars.

Newline inside

Column of stars

If cout << "\n" is inside the inner loop, each star lands on its own line. Use cout << "*" for stars; end the row only after the inner loop.

No newline

One endless line

Omitting the row break glues every star onto a single line.

rows = 1

Single star

Output is just * on one line — a good sanity check.

rows ≤ 0

Empty output

Outer loop never runs. Validate and re-prompt for interactive programs.

Bad input

Check cin.fail()

Letters leave cin in a failed state — prefer if (!(cin >> rows) || rows < 1).

Time and Space Complexity

ProgramTimeExtra space
Countdown nested loops (Examples 1–2)O(rows²)O(1)
Forward + string (Example 3)O(rows²)O(rows) temporary per row string

Total stars printed = n + (n - 1) + … + 1 = n(n+1)/2 — same as Program 1, still quadratic in n.

Key Takeaways

  • Rule: countdown i; row i prints exactly i stars.
  • Flip of Program 1: same inner loop — only reverse the outer loop.
  • Break the row: call cout << "\n" only after the inner loop.
  • Complexity: O(n²) time from the triangular star count; O(1) extra space for nested loops.

One line: for i from rows down to 1, print i stars with cout, then cout << "\n".

Frequently Asked Questions

The outer loop runs i from rows down to 1. For each i, the inner loop prints i stars. The first line uses i equal to rows so it is the longest; each later line has a smaller i, so the triangle points downward.
Program 1 uses for (i = 1; i <= rows; i++) so stars grow. This program uses for (i = rows; i >= 1; i--) so stars shrink. The inner loop still runs j from 1 to i.
Yes. Use for (i = 1; i <= rows; i++) and print (rows - i + 1) stars in the inner loop. Both styles produce the same shape.
cout << "*" stays on the same line. cout << "\n" ends the current line. Stars use cout << "*"; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows. Total stars are still n(n+1)/2, same as the upright triangle.
Yes. cout << string(i, '*') << "\n" prints a full row in one call while i counts down.
After cin >> rows, check cin.fail() or use if (!(cin >> rows)) to handle bad input.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

This inverted triangle uses the same inner loop as Program 1 — only the outer loop direction changes. Total stars stay n(n+1)/2, so complexity is still O(n²).

Next: Right-Aligned Triangle

Add leading spaces so the triangle grows against the right margin.

Program 3 tutorial →

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