Inverted 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 an inverted right-angled triangle where each next row contains one less star than the previous row.

Row i prints exactly i stars while i decreases 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 <= i; ++j) cout << "*";
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Streams setup

#include <iostream> and cout replace C’s printf. rows sets how many characters appear on the first line.

Setup
2

Outer loop: wide row first

for (i = rows; i >= 1; --i) counts down so the first line has rows stars and each following line has one fewer.

Outer
3

Inner loop: cout << "*"

for (j = 1; j <= i; ++j) streams exactly i asterisks on the current row—same inner bound as the growing triangle, opposite outer direction.

Inner
4

Newline

cout << "\n" ends each row after the star run completes.

Newline
=

Inverted triangle

Total stars remain n(n + 1)/2O(rows²) time, O(1) extra space. The widest line scrolls sideways in the mobile-friendly result strip.

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Use cin so row count is chosen at runtime
  • Switch the outer loop to 1..rows and print rows - i + 1 stars for the same shape
  • Print numbers (12345, 1234, …) instead of *
  • Build a hollow inverted triangle (border stars only)
  • Compare side-by-side with the standard right-angled triangle from Program 1

Avoid

  • Forgetting cout << "\n" or endl after each row
  • Mixing up i >= 1 and i > 0 without adjusting the inner bound
  • Using cin without checking that rows is positive
  • Assuming the inner loop must always run 1..i when a formula could also work
  • Hard-coding 5 when you meant to use the rows variable

Key Takeaways

1

The outer loop runs from rows down to 1; the inner loop prints that many stars per row.

2

Row with i = k prints exactly k stars—so the first row is the widest.

3

Time complexity is still O(n²) for n rows; only the loop direction changed.

4

You can implement the same shape with a forward outer loop and (rows - i + 1) stars in the inner loop.

5

This pattern pairs naturally with the upright triangle—master both to read nested loops in either direction.

❓ Frequently Asked Questions

The outer loop starts from rows and decrements to 1. For each i, the inner loop prints i stars, so the first line is longest and each line shrinks by one—an upside-down right triangle.
The standard triangle uses for (i = 1; i <= rows; ++i) (stars grow each row). This one uses for (i = rows; i >= 1; --i) (stars shrink each row).
Yes. Use for (i = 1; i <= rows; ++i) and print (rows - i + 1) stars in the inner loop. Both approaches yield the same output.
The time complexity is O(n²) where n is the number of rows. Total stars printed are still n(n+1)/2.

Next: Right-Aligned Triangle

Continue to Program 3 to print a right-aligned star triangle in C++.

Program 3 →
Did you know?

This pattern is the direct mirror of Program 1. Once you master both, try right-aligned versions (Programs 3 and 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.

15 people found this page helpful