C++ Sequential Number Triangle Pattern (Narrowing)

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

What Is This Pattern?

A decreasing continuous number triangle prints numbers in one unbroken sequence from 1, while each row prints one fewer value than the row above — left-aligned, no leading spaces.

Remember
Rule: k starts at 1; row i prints (rows - i + 1) numbers

  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15           ← 5 rows (setw(3) columns)

Unlike Program 35 (same continuous counter, but right-aligned with leading spaces), this shape sits flush left and simply shortens each row.

How to Solve It

Keep one counter outside the loops, shrink the inner bound each row, and format with setw(3).

MethodIdeaBest for
Counter + nested loopsOuter = rows; inner = rows..i with k++Learning, interviews, exams
setw(3) columnsFixed width keeps 1-digit and 2-digit values alignedReadable console output

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from rows down to i:
        print k in width 3 (no newline)
        k = k + 1
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; ++i)
Print shrinking countfor (j = rows; j >= i; --j)
Emit next numbercout << setw(3) << k++;
End the rowcout << "\n";
Need setw#include <iomanip>
Right-aligned twinProgram 35 — same k, leading spaces

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << setw(3) << k++Stays on the same lineEach number
cout << "\n"Ends the current lineAfter the inner loop

Print numbers without a newline, then end the row once. cout << endl also ends the line and flushes; "\n" is enough for these demos.

Live Preview

Change the row count and the decreasing continuous triangle updates instantly — including the triangular number total.

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

Live result 5 rows · 15 numbers
  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the shrinking inner range, and the numbers taken from k.

iInner jCountk valuesPrinted row
14..141..41 2 3 4
24..235..75 6 7
34..328..98 9
44..411010

Total prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. Never reset k between rows or you get a restarting triangle instead.

C++ Programs

Three complete programs: fixed rows, cin input, and a small trace demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — ideal for first demos and screenshots.

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

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

    for (i = 1; i <= rows; ++i)
    {
        for (j = rows; j >= i; --j)
            cout << setw(3) << k++;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Start the counter once. k = 1 lives outside both loops so the sequence never restarts.

2. Outer loop walks rows. i runs from 1 to rows — one shrinking line per iteration.

3. Inner loop shrinks. j runs from rows down to i, so row i prints rows - i + 1 numbers.

4. Format, then break the line. setw(3) keeps columns tidy; cout << "\n" only after the inner loop.

Example 2 — User Input Version

Read the row count at runtime. Prefer validating cin (shown in the tip below).

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

int main()
{
    int rows;
    int i, j, k = 1;

    cout << "Enter rows: ";
    cin >> rows;

    for (i = 1; i <= rows; ++i)
    {
        for (j = rows; j >= i; --j)
            cout << setw(3) << k++;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for a row count, then store it in rows.

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

3. Safer input tip. Prefer:

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

Example 3 — Compact rows = 3

Same counter and shrinking inner loop with a smaller row count for quick tracing on paper.

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

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

    for (i = 1; i <= rows; ++i)
    {
        for (j = rows; j >= i; --j)
            cout << setw(3) << k++;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Only rows changes. The counter and inner loop stay identical to Example 1.

2. Trace on paper. For i = 1, 2, 3 the counts are 3, 2, 1 — easy to check by hand before scaling up.

3. Same formatting. setw(3) still applies; with single digits you mainly see the leading pad spaces.

Edge Cases & Pitfalls

Check these before calling the solution done.

k reset

Restarting triangle

If you set k = 1 inside the outer loop, each row restarts at 1. Keep k outside both loops.

j = 1..i

Growing width

An ascending inner bound prints a normal growing triangle. Use j = rows; j >= i; --j to shrink.

\n early

Column of numbers

If cout << "\n" is inside the inner loop, each number lands on its own line. End the row only after the inner loop.

No setw

Misaligned digits

Without setw(3), single-digit and two-digit values drift. Include <iomanip> and keep a fixed width.

rows = 1

Single number

Output is just 1 (padded to width 3). A good sanity check.

Bad cin

Validate rows

Check cin >> rows and require rows >= 1 before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(rows²)O(1)

Total numbers printed = n + (n - 1) + … + 1 = n(n + 1)/2 — quadratic in n. Only a few integers of extra memory.

Key Takeaways

  • Rule: one counter k; row i prints rows - i + 1 values with setw(3).
  • vs Program 35: same continuous sequence — here left-aligned and shrinking; there right-aligned with pads.
  • Break the row: call cout << "\n" only after the inner loop.
  • Complexity: O(n²) time from the triangular print count; O(1) extra space.

One line: for i = 1..rows, print setw(3) << k++ while j runs rows..i, then end the line.

Frequently Asked Questions

Continuous numbers starting from 1, but each row has one fewer number: 5 on row 1, then 4, 3, 2, and 1 — totaling 15 numbers for 5 rows.
Counter k starts at 1 before the loops and increments with k++ each time a number prints — it is never reset inside the outer loop.
Row i prints rows - i + 1 numbers — the inner loop runs from j = rows down to i.
setw(3) reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
Program 35 is right-aligned with leading spaces. Program 38 is left-aligned with decreasing row width and the same continuous counter.
Printing with setw stays on the same line for each number. Printing a newline ends the current line. Numbers use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
After cin >> rows, check failure and require rows ≥ 1 before the outer loop.

Did you know?

A counter k starts at 1 and increments every time a number prints. Row i prints rows - i + 1 numbers with setw(3) — total prints = n(n+1)/2. Same continuous sequence as Program 35, but left-aligned with shrinking width.

Next: Rotating Number Pattern

Continue with the next pattern in the C++ number-pattern series.

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