C++ Decreasing Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned decreasing number triangle prints rows..i on each row — shorter rows get leading spaces so the right edge lines up.

Remember
Rule: for i from rows down to 1
        print 2 spaces, (i - 1) times
        print j from rows down to i (width 2)

         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1     ← rows = 5

Follows the continuous counter in Program 35; next is the centered square number triangle in Program 37.

How to Solve It

Outer loop shrinks i from rows to 1. First inner loop indents; second prints rows..i with setw(2).

MethodIdeaBest for
Indent + descending digitsSpaces for j < i, then j = rows..iLearning, interviews, exams
setw(2) columnsMatches pad width of " "Clean right alignment

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from 1 to i - 1:
        print 2 spaces
    for j from rows down to i:
        print j in width 2
    print newline

Cheat sheet

GoalPattern
Shrink start digitfor (i = rows; i >= 1; i--)
Indentfor (j = 1; j < i; j++) cout << " ";
Print rows..ifor (j = rows; j >= i; j--)
Fixed columnscout << setw(2) << j;
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << " " / setw(2) << jStays on the same lineEach pad or number on the row
cout << "\n"Ends the current lineAfter both inner loops finish

Print characters without a newline, then end the row once.

Live Preview

Change the row count and the right-aligned decreasing triangle updates instantly — capped at 9 for readable demos.

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

Live result rows = 5 · 15 numbers
         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1

Worked Walkthrough — rows = 5

Trace how indents shrink and each row always starts at 5, counting down to i.

iIndentsNumbersPrints
5455
435 45 4
325 4 35 4 3
215..25 4 3 2
105..15 4 3 2 1

Indents = i − 1. Numbers on the row = rows − i + 1. Total = n(n+1)/2 → O(n²).

C++ Programs

Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — indent loop, then descending digits with setw(2).

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

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

    for (i = rows; i >= 1; i--)
    {
        for (j = 1; j < i; j++)
            cout << "  ";

        for (j = rows; j >= i; j--)
            cout << setw(2) << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer starts at rows. First printed line is the shortest (i = 5 → just 5).

2. Indent then digits. Print i - 1 pairs of spaces, then rows..i with width 2.

3. Newline once. Call cout << "\n" only after both inner loops finish.

Example 2 — User Input Rows

Read rows with cin, validate, then use the same dual inner-loop core.

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

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

    cout << "Enter rows: ";
    if (!(cin >> rows) || rows <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }

    for (i = rows; i >= 1; i--)
    {
        for (j = 1; j < i; j++)
            cout << "  ";

        for (j = rows; j >= i; j--)
            cout << setw(2) << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Same core. Indent + setw(2) digits match Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable output:

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

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm indents and the restart at rows.

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

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

    for (i = rows; i >= 1; i--)
    {
        for (j = 1; j < i; j++)
            cout << "  ";

        for (j = rows; j >= i; j--)
            cout << setw(2) << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 3 → 3; i = 2 → 3 2; i = 1 → 3 2 1.

2. Trace on paper. If the number loop starts at i instead of rows, every row loses its shared peak digit.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

no indent

Missing space loop

Without the indent loop the triangle left-aligns. Keep j = 1..i-1 with " ".

j = i

Wrong number start

Starting the number loop at i instead of rows prints i..i only. Use j = rows..i.

width ≠ 2

Pad mismatch

Leading pads must match number width — use " " with setw(2).

\n inside

Broken rows

If cout << "\n" sits inside either inner loop, the row splits. Call it only after both finish.

rows = 1

Single value

Output is just 1 with no indents. A good sanity check for input validation.

cin

Check the stream

Validate cin >> rows before looping — a failed read leaves rows unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Row with start i prints rows − i + 1 numbers. Across all rows the total is 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Two inner loops: indent with spaces, then print rows..i.
  • Start at rows: every row begins at the peak digit and counts down to i.
  • setw(2): match pad width to number width so the right edge stays aligned.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each i from rows down to 1, indent then print rows..i with setw(2), then newline.

Frequently Asked Questions

A right-aligned decreasing triangle: for rows=5 the first line is 5, then 5 4, then 5 4 3, and so on until 5 4 3 2 1.
An indentation loop prints two spaces while j < i before the number loop runs, pushing shorter rows to the right.
setw(2) reserves 2 columns per number (right-aligned), keeping columns stable in the console output.
The number loop runs for (j = rows; j >= i; j--), so every row begins at rows and counts down to the current i.
Program 35 uses a continuous counter k. Program 36 restarts from rows on each row with a separate indentation loop.
cout << setw(2) << j stays on the same line. cout << "\n" ends the row after both inner loops finish.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.

Did you know?

Each row starts from rows and counts down to i. An indentation loop prints two spaces per step (j = 1..i-1), then setw(2) keeps number columns aligned.

Next: Centered Square Number Triangle

Continue with a centered pattern of square numbers.

Program 37 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