C Sequential Number Triangle Pattern (Narrowing)

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

What Is This Pattern?

A continuous decreasing-row triangle prints numbers from 1 onward without resetting, while each row is one number shorter. Row 1 has n values; the last row has one.

Remember
Rule: k = 1
      for i from 1 to rows
        for j from rows down to i
          print k with %3d; k++
        print newline

  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15                 ← rows = 5

Same continuous k idea as Program 35, but left-aligned and shrinking. Follows the palindrome triangle in Program 37; next is Program 39.

How to Solve It

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

MethodIdeaBest for
Counter + shrinkk++ inside j = rows..iLearning, interviews, exams
User input rowsSame loops; read rows with scanfReusable demos and labs

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from rows down to i:
        print k in width 3; k++
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Shrink the rowfor (j = rows; j >= i; j--) printf("%3d", k++);
End the rowprintf("\n");
Count on row irows - i + 1
Total numbersn(n + 1) / 2

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%3d", k++)Stays on the same lineEach number in the row
printf("\n")Ends the current lineAfter the inner loop

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

Live Preview

Change the row count and the decreasing-row continuous triangle updates instantly — each number uses a width-3 field like %3d.

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

Live result 5 rows · 15 numbers
  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, how many times the inner loop runs, and which values k takes.

iInner countNumbers from kPrinted row
141, 2, 3, 41 2 3 4
235, 6, 75 6 7
328, 98 9
411010

Total numbers for n rows = n(n + 1)/2 (here 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 — continuous counter with shrinking inner loop and %3d.

C
#include <stdio.h>

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Start the counter. k = 1 is declared once before the loops so the sequence never resets.

2. Shrink each row. Inner loop j = rows..i prints rows - i + 1 numbers with %3d.

3. End the line. printf("\n") runs only after the inner loop finishes.

When i = 2, four numbers start at 6; when i = 5, only 15 prints.

Example 2 — User Input Version

Read rows with scanf and reject invalid input. Same counter and shrink core.

C
#include <stdio.h>

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

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Exit early if scanf fails or rows < 1.

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

3. Safer input tip. Prefer an explicit message instead of a silent exit:

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 counter and shrinking inner loop with a smaller row count for quick paper tracing.

C
#include <stdio.h>

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only rows changes from 5 to 3 — k and the inner loop stay identical.

2. Trace on paper. For i = 2: inner runs twice → 4 then 5.

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.

reset k

Broken sequence

If you set k = 1 inside the outer loop, every row restarts at 1. Declare k once before both loops.

j = 1..i

Growing instead of shrinking

An ascending inner bound prints a widening triangle. Keep j = rows; j >= i; j--.

\n inside

Broken rows

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

no %3d

Misaligned columns

Plain printf("%d", k++) makes values like 10 crowd earlier columns. Keep %3d.

rows = 1

Smallest triangle

Output is a single width-3 1. A good sanity check for the counter and newline.

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 numbers = n(n + 1)/2 (triangular). For rows = 5 that is 15 printed values.

Key Takeaways

  • Rule: k = 1 once; inner j = rows..i prints %3d with k++.
  • Row length: row i prints rows - i + 1 numbers — always one fewer than the row above.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from the triangular sum; O(1) extra space.

One line: for each i, run j = rows..i printing %3d with k++, then printf("\n").

Frequently Asked Questions

Continuous numbers starting from 1, but each row has one fewer number: 5 on row 1, then 4, 3, 2, and 1 — totaling 15 numbers for 5 rows.
Counter k starts at 1 before the loops and increments with k++ each time a number prints — it is never reset inside the outer loop.
Row i prints rows - i + 1 numbers — the inner loop runs from j = rows down to i.
The format specifier reserves 3 columns per number, keeping columns aligned when values become two digits.
Program 35 is right-aligned with leading spaces. Program 38 is left-aligned with decreasing row width and the same continuous counter.
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?

A counter k starts at 1 and increments every time a number prints. Row i prints rows - i + 1 numbers with %3d — total prints = n(n+1)/2.

Next: Rotating Number Pattern

Continue with a row that rotates digits forward and wraps the leftover values.

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