Descending Reverse Number Triangle in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print the descending reverse number triangle pattern 54321, 4321, 321, 21, 1 in C++ using nested for loops.

The outer loop reduces the row length, and the inner loop counts backward from the current row limit down to 1.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
54321
4321
321
21
1
1

Complete C++ Program

Fixed five rows: the outer loop counts down; the inner loop prints from i down to 1 on each row.

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

int main() {
    int rows = 5;
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Choose the row count

int rows = 5; sets the maximum number and how many lines will be printed.

Setup
2

Outer loop (descending rows)

for (i = rows; i >= 1; i--) reduces the length of each line from rows down to 1.

Row control
3

Inner loop (print i..1)

for (j = i; j >= 1; j--) prints the reverse sequence on the current row using cout << j.

Number printing
4

New line

cout << "\n"; completes the row and moves to the next line.

Line break
=

Reverse descending triangle

Total numbers printed: n + (n-1) + … + 1 = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the maximum number (and the number of rows) using 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 = i; j >= 1; j--) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate input (e.g., check cin.fail()) before using rows
  • Add spaces between numbers with cout << j << ' '
  • Print the ascending version by changing the inner loop to count up from 1 to i
  • Right-align the triangle by printing leading spaces before each row
  • Replace numbers with characters to create alphabet patterns

Avoid

  • Forgetting to print a newline between rows
  • Using negative or zero rows without validating input
  • Mixing row/column logic (keep the outer loop for rows)
  • Flushing output with endl unnecessarily in tight loops

Key Takeaways

1

The outer loop decreases the row length from rows down to 1.

2

The inner loop prints a reverse sequence from i down to 1 on each row.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

This is a great stepping stone to patterns like Program 1 (ascending digits) and inverted/shifted triangles.

❓ Frequently Asked Questions

Because the inner loop counts down: for (j = i; j >= 1; j--). That makes the row print i, i-1, ..., 1.
Program 1 prints 12345, 1234, 123, ... (ascending on each row). This program prints 54321, 4321, 321, ... (descending on each row).
Yes. Replace cout << j; with cout << j << ' ';.
O(n²) for n rows: you print n+(n-1)+…+1 = n(n+1)/2 numbers.

Explore More C++ Number Patterns!

Try more variations to strengthen your nested-loop skills.

All Number Patterns →
Did you know?

You can generate many number patterns by changing just two things: the start and end values of the inner loop, and whether it counts up or down.

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