C++ Number Triangle Pattern (Fill with N)

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

What Is This Pattern?

A fill-with-5 number triangle prints an ascending sequence on each row, then pads the rest with the maximum value so every row has the same width n.

Remember
Rule: for i from n down to 1
        print j from i to n
        then print n, (i - 1) times

5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5     ← n = 5

Follows the alternating odd/even triangle in Program 18; next is the continuous number triangle in Program 20.

How to Solve It

Outer loop walks i from n down to 1. First inner loop prints the sequence; second pads with n.

MethodIdeaBest for
Sequence + fillPrint i..n, then pad i - 1 times with nLearning, interviews, exams
Custom fillSame sequence loop; second loop prints a separate fill valueVariants where padding ≠ n

Pseudocode

Pseudocode
for i from n down to 1:
    for j from i to n:
        print j and a space
    for j from 1 to i - 1:
        print n and a space
    print newline

Cheat sheet

GoalPattern
Walk rows top → bottomfor (i = n; i >= 1; i--)
Print sequencefor (j = i; j <= n; j++) cout << j << " ";
Pad with nfor (j = 1; j < i; j++) cout << n << " ";
End of rowcout << "\n";
Custom fillcout << fill << " "; in the second loop

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j << " "Stays on the same lineEach number on the row
cout << "\n"Ends the current lineAfter both inner loops finish

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

Live Preview

Change the width n and the fill-with-n 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 n = 5 · 25 numbers
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5

Worked Walkthrough — n = 5

Trace how each outer i builds a sequence, then pads to width 5.

iSequenceFill countPrints
5545 5 5 5 5
44 534 5 5 5 5
33 4 523 4 5 5 5
22 3 4 512 3 4 5 5
11 2 3 4 501 2 3 4 5

Every row prints exactly n numbers → total n² prints → O(n²).

C++ Programs

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

Example 1 — Fixed n = 5

Hard-coded width — sequence loop then fill loop on each descending row.

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

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

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

        for (j = 1; j < i; j++)
            cout << n << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer descends. i starts at n (all fill) and ends at 1 (full sequence).

2. Sequence then pad. Print j from i to n, then print n exactly i - 1 times.

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

Example 2 — User Input Width

Read n with cin, validate, then use the same sequence + fill core.

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

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

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

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

        for (j = 1; j < i; j++)
            cout << n << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

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

2. Same core. Sequence + fill matches Example 1 — only n comes from the user.

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

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

Example 3 — Compact n = 3

Same structure with width three — easy to confirm sequence vs fill counts.

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

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

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

        for (j = 1; j < i; j++)
            cout << n << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

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

2. Count the pads. Fill runs i - 1 times — zero pads when i = 1.

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

Edge Cases & Pitfalls

Check these before calling the solution done.

no fill

Uneven row widths

Skipping the second loop leaves short top rows. Always pad i - 1 times so every row has width n.

i++

Wrong outer direction

Ascending i from 1 to n flips the pattern unless you rewrite both loops. Keep i descending for this shape.

\n inside

Broken rows

If cout << "\n" sits inside either inner loop, each number lands on its own line. Call it only after both loops finish.

n = 1

Single number

Output is just 1 — a good sanity check for input validation.

no space

Digits glued together

Use cout << j << " " so multi-digit values stay readable as n grows.

cin

Check the stream

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

Time and Space Complexity

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

Each of n rows prints exactly n numbers → n² prints → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Descend first: outer i from n down to 1 builds the top-all-fill shape.
  • Two inner loops: sequence i..n, then pad i - 1 times with n.
  • Break the row: cout << j << " " in the inners; cout << "\n" after both.
  • Complexity: O(n²) time from n² prints; O(1) extra space.

One line: for each i from n down to 1, print i..n, pad with n, then newline.

Frequently Asked Questions

A fill-with-n rectangle of width n: for n=5 you get 5 5 5 5 5 / 4 5 5 5 5 / 3 4 5 5 5 / 2 3 4 5 5 / 1 2 3 4 5 — each row is a sequence then padded with n.
When i = 5, the sequence loop prints j = 5 once, then the fill loop runs 4 times — all values are 5, so the row is 5 5 5 5 5.
The first prints the increasing sequence i..n. The second fills remaining positions with n so every row has the same width.
When i = 1, the sequence loop prints j = 1 to 5 and the fill loop runs zero times — no padding needed.
cout << j << " " stays on the same line with a space. cout << "\n" ends the current line. Numbers use cout; the row break uses cout << "\n" after both inner loops.
Use a separate fill variable in the second loop only, while the sequence still runs up to n.
Rows will have different widths — the top row may be short while the bottom row is full length.
O(n²) for width n because each of n rows prints n numbers.

Did you know?

Each row prints an ascending sequence i..n, then pads with n so every row has width n. The second inner loop runs i - 1 times — still O(n²) total prints.

Next: Continuous Number Triangle

Continue with a running counter that spans every row.

Program 20 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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