C Number Triangle Pattern (Left-Shifted)

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

What Is This Pattern?

A left-shifted descending number triangle prints digits i..rows on each row, so every line starts one digit higher and the left edge appears to slide right.

Remember
Rule: for i = 1..n, print i..n

12345
2345
345
45
5       ← 5 rows

In C the outer loop picks the row start i, the inner loop prints j from i to rows, then printf("\n") moves to the next line. Compare with Program 1, which prints 1..i while counting the outer loop down.

How to Solve It

Master the nested loops first, then optionally space the digits for easier reading.

MethodIdeaBest for
Nested loopsOuter = start 1..n, inner = digits i..n via printfLearning, interviews, exams
Spaced digitsSame loops with printf("%d ", j)Clearer tracing by hand

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Pick row startfor (i = 1; i <= rows; i++)
Print digits i..rowsfor (j = i; j <= rows; j++) printf("%d", j);
End the rowprintf("\n");
Spaced formprintf("%d ", j); instead of printf("%d", j)
Program 1 shape insteadfor (i = rows; i >= 1; i--) with j = 1..i

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j)Stays on the same lineEach digit j
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 left-shifted triangle updates instantly — including the total digit count.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 digits
12345
2345
345
45
5

Worked Walkthrough — rows = 4

Trace each outer-loop value of i and the digits the inner loop prints.

iInner jPrinted rowDigits
11..412344
22..42343
33..4342
44..441

Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.

C Programs

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

Example 1 — Fixed rows = 5

Outer loop picks the start digit; inner loop prints from i through rows.

C
#include <stdio.h>

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Pick the start. for (i = 1; i <= rows; i++) yields 1, 2, 3, 4, 5 as row starts.

2. Print digits. When i = 1, the inner loop prints 12345; when i = 5, just 5.

3. Newline. printf("\n") after the inner loop starts the next shorter row.

Example 2 — User Input

Read the row count 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 whole number.\n");
        return 1;
    }

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

        printf("\n");
    }

    return 0;
}

How It Works

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

2. Same nested loops. Four rows start at 1234 and end at 4.

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 = i; j <= rows; 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.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong start

j = 1 instead of j = i

That prints 1..rows every row (a rectangle). Keep for (j = i; j <= rows; j++).

newline early

printf("%d\n", j) inside the inner loop

That puts each digit on its own line. Use printf("%d", j); call printf("\n") only after the row finishes.

off-by-one

j < rows instead of j <= rows

You drop the last digit on every row (e.g. 12345 becomes 1234).

rows = 1

Single 1

Output is just 1 — a good sanity check.

rows ≤ 0

Empty output

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

Bad input

Unchecked scanf

Always check scanf’s return value so non-numeric input does not leave rows uninitialized.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
Spaced digits (Example 3)O(n²)O(1)

There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.

Key Takeaways

  • Rule: for i = 1..n, print digits i..n with printf.
  • Start at i: the inner loop begins at j = i, which is what creates the left shift.
  • printf vs newline: digits stay on the line; printf("\n") advances after each row.
  • Next step: Program 3 reverses the digit order on each row.

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

Frequently Asked Questions

The inner loop starts at j = i, not j = 1. When i increases (1, 2, 3, …), each row begins at that value and prints until rows.
Because each next row starts at a higher i, numbers below i are not printed anymore — the triangle shifts left visually.
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits use printf; the row break uses printf("\n") after the inner loop.
Program 1 counts the outer loop down and prints 1..i. Program 2 counts up and prints i..rows — same total prints, different shape.
Change the rows variable or read it with scanf. The outer loop runs from 1 to rows, and the inner loop prints j from i to rows.
Yes. Use printf("%d ", j) instead of printf("%d", j) — see Example 3.
O(n²) for n rows because total prints are n+(n-1)+…+1 = n(n+1)/2.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows 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 starts at i and prints through rows, so the left edge shifts right each line — still O(n²) total prints for n rows.

Next: Reverse Descending Number Triangle

Keep a shrinking width, but print digits in reverse order on each row.

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