C Number Triangle Pattern (Consecutive Offset)

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

What Is This Pattern?

An increasing triangle from 1 prints values from the formula i + j - 1. Row i has i consecutive numbers starting at i, separated by spaces.

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

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9     ← rows = 5

Same shape as Program 32, different formula. Follows the triangle from 11 in Program 32; next is Program 34 (starts at 0).

How to Solve It

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

MethodIdeaBest for
Fixed formulaPrint i + j - 1 for j = 1..iLearning, interviews, exams
User input rowsSame loops; read rows with scanfReusable demos and labs

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print (i + j - 1) and a space
    print newline

Cheat sheet

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

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d ", i + j - 1)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 row count and the triangle updates instantly — each row starts at its row number via i + j - 1.

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

Live result 5 rows · 15 values
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9

Worked Walkthrough — rows = 4

Trace each outer-loop value of i and list the formula results for j = 1..i.

iValues (i + j - 1)Printed row
111
22, 32 3
33, 4, 53 4 5
44, 5, 6, 74 5 6 7

Total values for n rows = n(n + 1)/2 (here 4 × 5 / 2 = 10). That triangular sum is why time is O(n²).

C Programs

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

Example 1 — Fixed rows = 5

Hard-coded row count — classic i + j - 1 formula.

C
#include <stdio.h>

int main(void)
{
    int i, j;

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

        printf("\n");
    }

    return 0;
}

How It Works

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

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

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

When i = 1 you get 1; when i = 4 you get 4 5 6 7.

Example 2 — User Input Version

Read rows with scanf and clamp to a positive value. Same formula core.

C
#include <stdio.h>

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

    printf("Enter rows: ");
    scanf("%d", &rows);
    if (rows < 1) rows = 1;

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and clamp. Read rows, then force a minimum of 1 so the outer loop runs.

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

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

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

Example 3 — Compact rows = 3

Same formula with a smaller row count for quick paper tracing.

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 + j - 1);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only rows changes from 5 to 3 — the formula stays i + j - 1.

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

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.

wrong formula

Rows start at the wrong number

Using i + j or 9 + i + j shifts every value. Keep i + j - 1 so row i begins at i.

no space

Glued numbers

printf("%d", i + j - 1) without a space merges values like 23. 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.

j to rows

Rectangle instead of triangle

Looping j to rows instead of i prints the same count every line. Inner bound must be i.

rows = 1

Smallest triangle

Output is a single 1 — a good sanity check.

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(rows²)O(1)
Small demo (Example 3)O(rows²)O(1)

Total values = n(n + 1)/2 — quadratic in n. For rows = 5 that is 15 printed numbers.

Key Takeaways

  • Rule: for each i, print i + j - 1 for j = 1..i.
  • Row start: when j = 1, the value equals i — each row begins at its row number.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from n(n + 1)/2 values; O(1) extra space.

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

Frequently Asked Questions

Because when j = 1, the expression i + j - 1 becomes i. Row 4 therefore starts with 4.
j increases by 1, so i + j - 1 increases by 1 as well — producing consecutive numbers on each row.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
printf("%d ", i + j - 1) keeps values separated on the same row. printf("\n") ends the row.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Check scanf's return value and reject non-positive row counts so the outer loop has a valid range.

Did you know?

Each printed value is computed as i + j - 1. Row i = 1 prints 1; row i = 4 prints 4, 5, 6, 7 — a left-shifted increasing triangle starting at 1.

Next: Increasing Triangle from 0

Same growing shape — switch to zero-based loops and the formula i + j.

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