C Number Triangle Pattern (Repeated Digits)

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

What Is This Pattern?

A repeating number triangle prints the row index i exactly i times on each row — 1, 22, 333, 4444, and so on.

Remember
Rule: for i from 1 to rows
        for j from 1 to i: print i (no space)

1
22
333
4444
55555     ← rows = 5

Follows the reverse left-growing triangle in Program 8; next is the descending repeating triangle in Program 10.

How to Solve It

Outer loop picks the digit i. Inner loop runs i times and prints i each time — not j.

MethodIdeaBest for
Print i in the inner loopOuter i = 1..rows; inner j = 1..i; print iLearning, interviews, exams
Spaced digitsprintf("%d ", i) instead of printf("%d", i)Easier reading on each row

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print i
    print newline

Cheat sheet

GoalPattern
Pick each rowfor (i = 1; i <= rows; i++)
Repeat digitfor (j = 1; j <= i; j++) printf("%d", i);
End of rowprintf("\n");
Spaced formprintf("%d ", i);
vs Program 55 prints j; 9 prints i — same loop bounds

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", i)Stays on the same lineEach repeated digit on the row
printf("\n")Ends the current lineAfter the inner loop finishes

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

Live Preview

Change the row count and the repeating triangle updates instantly — capped at 9 for readable single-digit 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 digits
1
22
333
4444
55555

Worked Walkthrough — rows = 5

Trace how the inner loop count equals the digit being printed.

iInner runsPrints
11 time1
22 times22
33 times333
55 times55555

Total digits = n(n+1)/2 → O(n²). The key mistake to avoid: printing j instead of i.

C Programs

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

Example 1 — Fixed rows = 5

Hard-coded height — print i inside the inner loop, not j.

C
#include <stdio.h>

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

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

    return 0;
}

How It Works

1. Outer picks the digit. i is both the row index and the value to repeat.

2. Inner repeats it. j only controls how many times — it is never printed.

3. Newline. Call printf("\n") only after the inner loop finishes the row.

Example 2 — User Input Rows

Read rows with scanf, validate, then run the same nested loops.

C
#include <stdio.h>

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

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

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

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input before printing.

2. Same core. Outer i / inner print-i matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable single-digit output:

Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
    printf("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 you print i, not j.

C
#include <stdio.h>

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

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

    return 0;
}

How It Works

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

2. Trace on paper. If you accidentally print j, row 3 becomes 123 (Program 5) — not 333.

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.

printf j

Got Program 5 instead

If you print j instead of i, you get 1, 12, 123… Always print the outer variable.

i = rows..1

Descending repeats

Counting the outer loop down gives Program 10’s shape (5, 44, 333…). Keep outer 1..rows here.

\n inside

Broken rows

If printf("\n") sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.

rows = 1

Single digit

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

rows > 9

Multi-digit width

Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced %d.

scanf

Check the return value

Validate scanf before looping — a failed read leaves rows uninitialized.

Time and Space Complexity

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

Total digits printed = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Print i, not j: outer picks the digit; inner only counts repeats.
  • Same bound as Program 5: j = 1..i, but the printed value is i.
  • Break the row: printf("%d", i) in the inner loop; printf("\n") after it.
  • Complexity: O(n²) time from n(n+1)/2 digits; O(1) extra space.

One line: for each i, print i exactly i times, then newline.

Frequently Asked Questions

A repeating number triangle: row 1 shows 1, row 2 shows 22, row 3 shows 333, and so on until row n shows the digit n repeated n times.
The inner loop prints the current row number i each time. It runs i times, so i is repeated i times on that row.
When i = 4, the inner loop runs 4 times and prints 4 each time.
Program 8 prints descending digits rows..i (5, 54, 543). Program 9 repeats the row digit i times (1, 22, 333).
Program 5 prints 1..i ascending per row. Program 9 prints i repeated i times — same inner bound, different print value.
printf("%d", i) stays on the same line. printf("\n") ends the row after the inner loop.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.

Did you know?

Each row repeats the row number i exactly i times — outer i runs 1..rows, inner j prints i on every iteration — producing 1, 22, 333, and so on. Total prints grow as O(n²).

Next: Descending Repeating Triangle

Continue with rows that repeat a descending digit (5, 44, 333).

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