C Incremental Number Triangle Pattern (Right-Aligned)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned incremental triangle prints a continuous counter across rows — 1, then 2 3, then 4 5 6, and so on — padded on the left with spaces and formatted with %3d.
Remember
Rule: k = 1
for i from 1 to rows
for j from rows down to 1:
if j > i: print 3 spaces
else: print k with %3d; k++
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 ← rows = 5
Fixed-width countdown plus a lasting counter. Follows the zero-based triangle in Program 34; next is Program 36.
Approach
How to Solve It
Start with fixed rows = 5, then generalize with scanf — optionally demo a smaller count for tracing.
Method
Idea
Best for
Counter + %3d
Spaces while j > i; else print k++ in width 3
Learning, interviews, exams
User input rows
Same loops; read rows with scanf
Reusable demos and labs
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from rows down to 1:
if j > i: print 3 spaces
else: print k (width 3); k = k + 1
print newline
Cheat sheet
Goal
Pattern
Start the counter
int k = 1;
Walk each row
for (i = 1; i <= rows; i++)
Fixed column width
for (j = rows; j >= 1; j--)
Leading spaces
if (j > i) printf(" ");
Print next number
printf("%3d", k++);
End the row
printf("\n");
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf(" ") / printf("%3d", k++)
Stays on the same line
Padding spaces and each number
printf("\n")
Ends the current line
After the inner loop
Print spaces and numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned incremental 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 result5 rows · 15 numbers
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i, the leading space groups, and the numbers taken from k.
i
Space groups
Numbers from k
Printed row
1
3
1
1
2
2
2, 3
2 3
3
1
4, 5, 6
4 5 6
4
0
7, 8, 9, 10
7 8 9 10
Each of n rows prints n width-3 cells → n² cells total. Numbers alone sum to n(n + 1)/2 — why time is O(n²).
Code
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 %3d and three-space padding.
C
#include <stdio.h>
int main(void)
{
int i, j, k = 1;
for (i = 1; i <= 5; i++)
{
for (j = 5; j >= 1; j--)
{
if (j > i)
printf(" ");
else
printf("%3d", k++);
}
printf("\n");
}
return 0;
}
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
How It Works
1. Outer loop grows i.i runs from 1 to 5 — one more number each row.
2. Fixed-width countdown.j always runs 5..1; print three spaces when j > i.
3. Continuous counter. Otherwise print k with %3d and increment — k never resets.
When i = 1 you get a lone padded 1; when i = 5 you get 11 through 15 with no leading pad groups.
Example 2 — User Input Version
Read rows with scanf and reject invalid input. Same counter and %3d 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 >= 1; j--)
{
if (j > i)
printf(" ");
else
printf("%3d", k++);
}
printf("\n");
}
return 0;
}
Output (when user enters 3)
Enter rows: 3
1
2 3
4 5 6
How It Works
1. Prompt and validate. Exit early if scanf fails or rows < 1.
2. Same core. Only the hard-coded 5 becomes rows in both loop bounds.
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 spacing logic 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 >= 1; j--)
{
if (j > i)
printf(" ");
else
printf("%3d", k++);
}
printf("\n");
}
return 0;
}
Output
1
2 3
4 5 6
How It Works
1. Same structure. Only rows changes from 5 to 3 — padding and %3d stay identical.
2. Trace on paper. For i = 2: one space group, then 2 and 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.
k reset
Counter restarts each row
Declaring or resetting k inside the outer loop reprints 1 every row. Keep k = 1 before both loops.
1 space
Broken column alignment
A single space instead of " " does not match %3d width. Always print three spaces while j > i.
no %3d
Two-digit drift
Plain printf("%d", k++) makes values like 10 crowd earlier columns. Keep %3d.
\n inside
Broken rows
If printf("\n") sits inside the inner loop, each cell lands on its own line. Call the newline only after the row finishes.
rows = 1
Smallest triangle
Output is a single padded 1 — a good sanity check.
scanf
Check the return value
If scanf fails, rows may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(rows²)
O(1)
Small demo (Example 3)
O(rows²)
O(1)
Each of n rows prints n width-3 cells → n² cells. Numbers printed = n(n + 1)/2. Extra memory stays constant aside from loop counters.
Remember
Key Takeaways
Rule: spaces while j > i; else printf("%3d", k++).
Keep k alive: declare k = 1 once before the outer loop so the sequence never resets.
Match widths: three spaces for pads and %3d for numbers so columns stay aligned.
Complexity:O(n²) time from n² cells; O(1) extra space.
One line: for each i, countdown j printing three spaces or %3d with k++, then printf("\n").
Frequently Asked Questions
Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
While j > i the program prints three spaces. That pads the left side so numbers shift right as rows grow.
The format specifier reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is declared outside the loops and increments with k++ each time a number prints, so the sequence continues across rows.
Program 30 prints descending digits per row. Program 35 uses a continuous counter k with fixed-width formatting.
Replace 5 with rows in both loop bounds — see Example 2.
O(n²) for n rows because each row runs a fixed-width inner loop of n iterations (and total numbers printed equal 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 is printed. Leading spaces appear while j > i, and %3d keeps columns aligned as values grow past single digits.