C Number Triangle Pattern (Fill with N)

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

What Is This Pattern?

A fill-with-n number triangle prints ascending digits i..n on each row, then pads the rest with n so every row has the same width.

Remember
Rule: for i = n..1, print i..n, then pad with n (width = n)

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

In C you use a descending outer loop, a sequence loop j = i..n, a fill loop that prints n for i - 1 slots, then printf("\n") ends the row. Classic demos hard-code n = 5; the same logic works for any width.

How to Solve It

Two inner loops are the whole trick — sequence first, then pad to fixed width.

MethodIdeaBest for
Pad with nSequence i..n, then fill remaining slots with nLearning, interviews, exams
Custom fillSame structure; second loop prints a different constantQuick variant once width clicks

Pseudocode

Pseudocode
for i from n down to 1:
    for j from i to n:
        print j and a space
    repeat (i - 1) times:
        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++) printf("%d ", j);
Pad with nfor (j = 1; j < i; j++) printf("%d ", n);
End the rowprintf("\n");
Custom pad valueprintf("%d ", fill); in the second loop

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d ", j)Stays on the same lineEach number plus a space
printf("\n")Ends the current lineAfter both inner loops

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

Live Preview

Change the width n and the filled triangle updates instantly — every row still has n numbers.

Whole numbers from 1 to 9 (single digits). 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 = 4

Trace each outer-loop value of i, the sequence printed, and how many pads are added.

iSequencePadsPrinted row
443 × 44 4 4 4
33 42 × 43 4 4 4
22 3 41 × 42 3 4 4
11 2 3 401 2 3 4

Every row has width n, so total prints are n × n = n².

C Programs

Three complete programs: fixed n = 5, user input, and a custom fill constant. Use View Output for sample results.

Example 1 — Fixed n = 5

Outer loop counts down; first inner loop prints the sequence; second pads with n.

C
#include <stdio.h>

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

    for (i = n; i >= 1; i--)
    {
        for (j = i; j <= n; j++)
            printf("%d ", j);

        for (j = 1; j < i; j++)
            printf("%d ", n);

        printf("\n");
    }

    return 0;
}

How It Works

1. Count down. for (i = n; i >= 1; i--) starts with a full-fill top row and ends with 1..n.

2. Sequence then pad. When i = 3, print 3 4 5, then two more 5s so the width stays 5.

3. Newline. printf("\n") after both inner loops starts the next row.

Example 2 — User Input

Read the triangle width with scanf and reject non-positive values.

C
#include <stdio.h>

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

    printf("Enter the triangle width: ");
    if (scanf("%d", &n) != 1 || n < 1)
    {
        printf("Please enter a positive whole number.\n");
        return 1;
    }

    for (i = n; i >= 1; i--)
    {
        for (j = i; j <= n; j++)
            printf("%d ", j);

        for (j = 1; j < i; j++)
            printf("%d ", n);

        printf("\n");
    }

    return 0;
}

How It Works

1. Validate n. Check scanf; require n >= 1.

2. Same nested loops. With n = 4, every row has four numbers ending in pads of 4 until the last row.

Example 3 — Custom Fill Constant

Pad with fill = 9 while the sequence still runs up to n = 5.

C
#include <stdio.h>

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

    for (i = n; i >= 1; i--)
    {
        for (j = i; j <= n; j++)
            printf("%d ", j);

        for (j = 1; j < i; j++)
            printf("%d ", fill);

        printf("\n");
    }

    return 0;
}

How It Works

1. Separate fill. The sequence still uses j up to n; only the padding prints fill.

2. Same width. Each row still has five numbers — compare with Example 1 to see the pad change.

Edge Cases & Pitfalls

Check these before calling the solution done.

missing pad

Forgetting the second inner loop

Rows shrink instead of staying width n. Keep the fill loop for i - 1 pads.

wrong bound

j <= i instead of j < i in the fill loop

That adds one extra pad and makes rows wider than n.

newline early

printf("\n") between the two inner loops

That splits sequence and pads onto different lines. Call printf("\n") only after both loops finish.

n = 1

Single cell

Output is just 1 — a good sanity check.

n ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

n > 9

Spaces still separate values, but columns look uneven. Labs usually stick to 1–9.

Time and Space Complexity

ProgramTimeExtra space
Two inner loops (Examples 1–3)O(n²)O(1)

Each of n rows prints exactly n numbers, so total work is n².

Key Takeaways

  • Rule: for i = n..1, print i..n, then pad i - 1 times with n.
  • Two loops: sequence first, fill second — that is what keeps every row width n.
  • printf vs newline: numbers stay on the line with a space; printf("\n") advances after both loops.
  • Next step: Program 20 prints a continuous counting triangle.

One line: for each i from n down to 1, print i..n, pad with n, then printf("\n").

Frequently Asked Questions

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 inner loop 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.
printf("%d ", j) stays on the same line. printf("\n") ends the current line. Numbers use printf with a trailing space; the row break uses printf("\n") after both inner loops.
Replace the hard-coded 5 with a variable n (see Example 2) or any constant you prefer for padding (see Example 3).
Rows will have different widths — the top row may be short while the bottom row is full length.
Yes, but print rows in reverse order or adjust i from 1 to n and change which row prints first.
O(n²) for width n because each of n rows prints n numbers.
Check scanf's return value: if (scanf("%d", &n) != 1) handle bad input. Unchecked scanf leaves n uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

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

Keep a single counter running across every row.

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