Right-Aligned Descending Number Triangle in C++

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

What You’ll Learn

How to print a right-aligned descending number triangle in C++: each row ends with a descending sequence like 21, 321, 4321, etc.

The key idea is to print spaces first for positions that should be empty, then print numbers once the condition is met.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
    1
   21
  321
 4321
54321
1

Complete C++ Program

The inner loop runs from rows down to 1. For each row i, print a space when j > i, otherwise print j.

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

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

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

    return 0;
}

🧠 How It Works

1

Set the size

int rows = 5; defines how many lines to print.

Setup
2

Outer loop controls the row

for (i = 1; i <= rows; i++) increases how many digits appear on each line.

Row control
3

Inner loop scans from rows down to 1

for (j = rows; j >= 1; j--) decides what to print at each position.

Column control
4

Spaces then digits

If j > i, print a space. Otherwise, print j. That right-aligns the triangle while producing i..1.

Condition
=

Right-aligned triangle

There are rows iterations of the inner loop for each row, so time complexity is O(n²).

2

Variation — User Input Version

Let the user decide 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 = 1; i <= rows; i++) {
        for (j = rows; j >= 1; j--) {
            if (j > i)
                cout << " ";
            else
                cout << j;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits using cout << j << ' '
  • Print the same pattern left-aligned by skipping the space branch
  • Replace digits with characters to generate alphabet triangles
  • Print an inverted version by iterating i from rows down to 1
  • Use formatting (like setw) for consistent alignment in wider patterns

Avoid

  • Forgetting the newline after each row
  • Hard-coding the value 5 in multiple places instead of using rows
  • Mixing tabs and spaces for alignment (tabs can render differently)
  • Using endl in loops unnecessarily (it flushes output)

Key Takeaways

1

Print leading spaces when j > i to right-align the triangle.

2

Print the descending digits i, i-1, ..., 1 when j <= i.

3

The inner loop always runs from rows down to 1, so each row has a fixed width.

4

This pattern is a great intro to alignment logic used in many console patterns.

❓ Frequently Asked Questions

Because the inner loop always iterates from rows down to 1, printing either a space or a digit each time. That guarantees a fixed number of characters per row.
Print only the descending digits for each row by looping j from i down to 1 (and don’t print leading spaces).
Yes. Change what you print when j <= i (for example print rows - j + 1) to shift the digit values.
O(n²) for n rows: n iterations of the inner loop for each of n rows.

Explore More C++ Number Patterns!

Next, try combining right-alignment with palindromes or stars to create richer shapes.

All Number Patterns →
Did you know?

Right-alignment in console patterns usually comes down to printing leading spaces. Once you master that, many pyramid and diamond patterns become much easier.

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