C Ascending Number Triangle Pattern

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

What Is This Pattern?

An ascending number triangle starts with one digit on row 1 and grows by one digit each row: row i prints 1 through i with no spaces between digits.

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

1
12
123
1234
12345     ← rows = 5

Follows the left-aligned descending triangle in Program 4; next is the reverse ascending triangle in Program 6.

How to Solve It

Outer loop counts up from 1 to rows. Inner loop prints digits 1..i. End each row with printf("\n").

MethodIdeaBest for
Growing inner boundOuter i = 1..rows; inner j = 1..i; print jLearning, interviews, exams
Spaced digitsprintf("%d ", j) instead of printf("%d", j)Easier reading on each row

Pseudocode

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

Cheat sheet

GoalPattern
Pick each rowfor (i = 1; i <= rows; i++)
Print digitsfor (j = 1; j <= i; j++) printf("%d", j);
End of rowprintf("\n");
Spaced formprintf("%d ", j);
Shrinking versionfor (i = rows; i >= 1; i--) (Program 1)

Printing Numbers vs Starting a New Line

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

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

Live Preview

Change the row count and the ascending triangle updates instantly — capped at 9 so every digit stays a single character.

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
12
123
1234
12345

Worked Walkthrough — Row i = 4

Trace the inner loop when the outer index is 4 — four digits with no spaces.

jPrintsLine so far
111
2212
33123
441234

Full row: 1234. For n rows, total digits = n(n+1)/2 → O(n²).

C Programs

Three complete programs: fixed rows, scanf input, and a spaced-digit variant. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — digits printed tight with printf("%d", 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", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop. i runs from 1 to 5 — each pass is one row.

2. Inner loop. j runs from 1 to i, so row i has exactly i digits.

3. Tight print. printf("%d", j) keeps digits on one line; printf("\n") starts the next row.

Example 2 — User Input Rows

Read rows with scanf and reject non-positive values.

C
#include <stdio.h>

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

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input with a clear message.

2. Same core. Only the outer loop limit comes from the user — digit logic stays identical.

3. Safer input tip. Cap demos to single-digit rows:

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 — Spaced Digits

Same loops with printf("%d ", j) so digits are easier to read.

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 ", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only the format string changes — loops stay identical to Example 1.

2. Space after each digit. printf("%d ", j) separates values for clearer scanning.

3. When to use it. Prefer the tight form for classic screenshots; use spaced form when teaching or tracing by hand.

Edge Cases & Pitfalls

Check these before calling the solution done.

i--

Shrinking instead of growing

Counting i down from rows prints the wide row first (Program 1). Use for (i = 1; i <= rows; i++) to grow.

\n inside

Broken rows

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

j <= rows

Rectangle instead of triangle

If the inner loop uses j <= rows every row, every line prints 1..rows. Keep j <= i.

rows = 1

Single row

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

rows > 9

Multi-digit values

Past 9, values like 10 take two characters and break the tight look. Cap demos at 9 or switch to spaced / fixed-width format.

scanf

Check the return value

If scanf fails, rows may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

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

Total digits = 1 + 2 + … + n = n(n+1)/2. For n = 5 that is 15 prints.

Key Takeaways

  • Rule: on row i, print digits 1 through i.
  • Grow upward: outer loop counts 1..rows so each row adds one digit.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from n(n+1)/2 digits; O(1) extra space.

One line: for i = 1..rows, print j for j = 1..i, then printf("\n").

Frequently Asked Questions

The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints j. Row 1 prints 1, row 2 prints 12, and so on until row rows prints 1..rows.
Because the outer loop counts up and the inner bound equals i. When i increases, each row prints one more digit than the previous row.
When i = 1, the inner loop runs j from 1 to 1 — exactly one digit.
printf("%d", j) stays on the same line. printf("\n") ends the current row after the inner loop.
Program 4 prints rows..i in descending order (54321, 5432, ...). Program 5 prints 1..i in ascending order — a growing triangle instead of a shrinking one.
Replace 5 with rows in the outer loop bound — see Example 2.
Count the outer loop down: for (i = rows; i >= 1; i--). Keep the inner loop as for (j = 1; j <= i; j++) — that is Program 1.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.

Did you know?

Row i prints digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — still O(n²) total prints.

Next: Reverse Ascending Triangle

Continue with rows that start mid-sequence and grow to the full width (5, 45, 345, 2345, 12345).

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