C Number Triangle Pattern (Starting from 0)

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

What Is This Pattern?

An increasing triangle from 0 prints values from the formula i + j with zero-based loops. Row i has i + 1 consecutive numbers starting at i.

Remember
Rule: for i from 0 to max
      for j from 0 to i:
        print (i + j) + space

0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10     ← max = 5

Same growing shape as Program 33, but loops start at 0. Follows the triangle from 1 in Program 33; next is Program 35.

How to Solve It

Start with fixed max = 5, then generalize with scanf — optionally demo a smaller max for tracing.

MethodIdeaBest for
Fixed formulaPrint i + j for j = 0..i, i = 0..maxLearning, interviews, exams
User input maxSame loops; read max with scanfReusable demos and labs

Pseudocode

Pseudocode
for i from 0 to max:
    for j from 0 to i:
        print (i + j) and a space
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 0; i <= max; i++)
Grow row lengthfor (j = 0; j <= i; j++)
Print value + spaceprintf("%d ", i + j);
End the rowprintf("\n");
Start at 1 insteadprintf("%d ", i + j - 1); with i = 1..rows — see Program 33

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d ", i + j)Stays on the same lineEach value plus a separator space
printf("\n")Ends the current lineAfter the inner loop

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

Live Preview

Change the max index and the triangle updates instantly — rows run from i = 0 to i = max.

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

Live result max 5 · 6 rows · 21 values
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10

Worked Walkthrough — max = 3

Trace each outer-loop value of i from 0 to 3 and list the formula results for j = 0..i.

iValues (i + j)Printed row
000
11, 21 2
22, 3, 42 3 4
33, 4, 5, 63 4 5 6

With max = n you get n + 1 rows and (n + 1)(n + 2)/2 values (here 4 × 5 / 2 = 10). That triangular sum is why time is O(n²).

C Programs

Three complete programs: fixed max, scanf input, and a small demo. Use View Output to reveal sample results.

Example 1 — Fixed max = 5

Hard-coded max index — classic i + j formula with zero-based loops.

C
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 0; i <= 5; i++)
    {
        for (j = 0; j <= i; j++)
            printf("%d ", i + j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop grows i. i runs from 0 to 5 — six rows, each one value longer.

2. Inner loop prints the formula. j runs from 0 to i; each value is i + j plus a space.

3. Newline. printf("\n") ends the row after the inner loop.

When i = 0 you get 0; when i = 2 you get 2 3 4.

Example 2 — User Input Version

Read max with scanf and reject negative values. Same formula core.

C
#include <stdio.h>

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

    printf("Enter max i: ");
    scanf("%d", &max);
    if (max < 0) max = 0;

    for (i = 0; i <= max; i++)
    {
        for (j = 0; j <= i; j++)
            printf("%d ", i + j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and clamp. Read max, then force a minimum of 0 so at least one row prints.

2. Same nested core. Only the hard-coded 5 becomes the user value.

3. Safer input tip. Unchecked scanf leaves max uninitialized on bad input. Prefer:

Safer input
if (scanf("%d", &max) != 1 || max < 0)
{
    printf("Enter a whole number max of 0 or greater.\n");
    return 1;
}

Example 3 — Compact max = 2

Same formula with a smaller max for quick paper tracing.

C
#include <stdio.h>

int main(void)
{
    int max = 2;
    int i, j;

    for (i = 0; i <= max; i++)
    {
        for (j = 0; j <= i; j++)
            printf("%d ", i + j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only max changes from 5 to 2 — the formula stays i + j.

2. Trace on paper. For i = 1: 1+0 = 1, 1+1 = 2 → 1 2.

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

Edge Cases & Pitfalls

Check these before calling the solution done.

i = 1

Missing the 0 row

Starting i at 1 skips the leading 0. Keep for (i = 0; i <= max; i++).

wrong formula

Rows start at the wrong number

Using i + j - 1 shifts every value like Program 33. Keep i + j so the first cell is 0.

no space

Glued numbers

printf("%d", i + j) without a space merges values like 12. Always append a space in the format string.

\n inside

Broken rows

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

max = 0

Smallest triangle

Output is a single 0 — a good sanity check.

scanf

Check the return value

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

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(max²)O(1)
Small demo (Example 3)O(max²)O(1)

With max = n, total values = (n + 1)(n + 2)/2 — quadratic in n. For max = 5 that is 21 printed numbers across 6 rows.

Key Takeaways

  • Rule: for each i from 0 to max, print i + j for j = 0..i.
  • Zero-based: starting at i = 0, j = 0 makes the first value 0.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from (n+1)(n+2)/2 values; O(1) extra space.

One line: for i from 0 to max, print i + j for j = 0..i, then printf("\n").

Frequently Asked Questions

Because the loops start at i = 0 and j = 0, so i + j = 0.
j increases from 0 to i, so i + j increases by 1 each step — producing consecutive numbers.
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Program 34 uses the formula i + j per cell. Program 35 is a right-aligned continuous counter triangle.
printf("%d ", i + j) keeps values separated on the same row. printf("\n") ends the row.
Replace 5 with max in the outer loop bound — see Example 2. Rows run from i = 0 to i = max.
O(n²) for max = n because total prints are 1 + 2 + ... + (n+1) = (n+1)(n+2)/2.
Check scanf's return value and reject negative max so the outer loop has a valid range.

Did you know?

Each printed value is computed as i + j. With i = 0 and j = 0 the first row prints 0; row i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.

Next: Right-Aligned Incremental Triangle

Continue with a continuous counter and right-aligned formatting using %3d.

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