C Decreasing Number Triangle Pattern (Right-Aligned)

Definition
What Is This Pattern?
A right-aligned decreasing triangle prints rows down to i on each row. An indent loop adds two spaces per step, and %2d keeps columns aligned.
Rule: for i from rows down to 1
print 2 spaces for j = 1..i-1
print j with %2d for j = rows..i
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1 ← rows = 5
Two inner loops — indent then numbers. Follows the incremental triangle in Program 35; next is Program 37.
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 |
|---|
Indent + %2d | Spaces for j < i; then print rows..i | Learning, interviews, exams |
| User input rows | Same loops; read rows with scanf | Reusable demos and labs |
Pseudocode
for i from rows down to 1:
for j from 1 to i-1:
print 2 spaces
for j from rows down to i:
print j (width 2)
print newline
Cheat sheet
| Goal | Pattern |
|---|
| Shrink outer bound | for (i = rows; i >= 1; i--) |
| Indent the row | for (j = 1; j < i; j++) printf(" "); |
Print rows..i | for (j = rows; j >= i; j--) printf("%2d", j); |
| End the row | printf("\n"); |
| Continuous counter instead | See Program 35 — k++ with %3d |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
printf(" ") / printf("%2d", j) | Stays on the same line | Indent spaces and each number |
printf("\n") | Ends the current line | After both inner loops |
Print spaces and numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned decreasing triangle updates instantly — each number uses a width-2 field like %2d.
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i from 4 down to 1, listing indent steps and numbers printed.
i | Indent steps | Numbers (rows..i) | Printed row |
|---|
4 | 3 | 4 | 4 |
3 | 2 | 4, 3 | 4 3 |
2 | 1 | 4, 3, 2 | 4 3 2 |
1 | 0 | 4, 3, 2, 1 | 4 3 2 1 |
Numbers printed for n rows = n(n + 1)/2. Indent work is also triangular — 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 — indent loop then %2d countdown from rows.
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = 1; j < i; j++)
printf(" ");
for (j = rows; j >= i; j--)
printf("%2d", j);
printf("\n");
}
return 0;
}
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1How It Works
1. Outer loop shrinks i. i runs from 5 down to 1 — first row is shortest.
2. Indent loop. j runs from 1 to i - 1, printing two spaces each time.
3. Number loop. j runs from rows down to i with %2d.
When i = 5 you get a padded 5; when i = 1 you get 5 4 3 2 1 with no indent.
Example 2 — User Input Version
Read rows with scanf and reject invalid input. Same dual inner-loop core.
#include <stdio.h>
int main(void)
{
int rows;
int i, j;
printf("Enter rows: ");
if (scanf("%d", &rows) != 1 || rows < 1)
return 0;
for (i = rows; i >= 1; i--)
{
for (j = 1; j < i; j++)
printf(" ");
for (j = rows; j >= i; j--)
printf("%2d", j);
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 rows in every loop bound.
3. Safer input tip. Prefer an explicit message instead of a silent exit:
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Enter a positive whole number of rows.\n");
return 1;
}
Example 3 — Compact rows = 3
Same indent and number loops with a smaller row count for quick paper tracing.
#include <stdio.h>
int main(void)
{
int rows = 3;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = 1; j < i; j++)
printf(" ");
for (j = rows; j >= i; j--)
printf("%2d", j);
printf("\n");
}
return 0;
}
How It Works
1. Same structure. Only rows changes from 5 to 3 — indent and %2d stay identical.
2. Trace on paper. For i = 2: one indent pair, then 3 and 2 → 3 2.
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.
i++Upside-down triangle
If the outer loop counts up, the longest row prints first. Use for (i = rows; i >= 1; i--).
1 spaceBroken column alignment
A single space instead of " " does not match %2d width. Always print two spaces per indent step.
no %2dMulti-digit drift
Plain printf("%d", j) crowds columns when values reach 10+. Keep %2d.
\n insideBroken rows
If printf("\n") sits inside either inner loop, each cell lands on its own line. Call the newline only after both loops finish.
rows = 1Smallest triangle
Output is a single padded 1 with no indent — a good sanity check.
scanfCheck 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) |
Numbers printed = n(n + 1)/2; indent steps form another triangular sum. Extra memory stays constant aside from loop counters.
Remember
Key Takeaways
Rule: indent with " " for j < i, then print rows..i with %2d.
Start at rows: i = rows makes the first line a single padded digit.
Match widths: two spaces for indents and %2d for numbers so columns stay aligned.
Complexity: O(n²) time; O(1) extra space.
One line: for i from rows down to 1, indent i - 1 times, print rows..i with %2d, then printf("\n").
Frequently Asked Questions
A right-aligned decreasing triangle: the first row prints 5, then 5 4, then 5 4 3, down to 5 4 3 2 1.
An indentation loop prints two spaces while j < i before the number loop runs, pushing shorter rows to the right.
The format specifier reserves 2 columns per number (right-aligned), keeping columns stable in the console output.
The number loop runs for (j = rows; j >= i; j--), so every row begins at rows and counts down to the current i.
Program 35 uses a continuous counter k. Program 36 restarts from rows on each row with a separate indentation loop.
Replace 5 with rows in the outer and number-loop bounds — see Example 2.
O(n²) for n rows because total indent steps plus number prints grow quadratically; numbers alone sum to 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 row starts from rows and counts down to i. An indentation loop prints two spaces per step (j = 1..i-1), then %2d keeps number columns aligned.
Next: Palindrome Number Triangle
Continue with a triangle that mirrors descending and ascending digits on each row.
Program 37 tutorial →About the author
Developer, cloud engineer, and technical writer
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