Shape Rule
i..1 per row
Row with outer i = 1 prints 1; row with i = 5 prints 54321 — digits grow from the right.

Program 7 prints a reverse row number triangle: each row shows digits from the current row index down to 1 — 1, 21, 321, and so on. This tutorial covers the shape rule, ascending outer loop, inner countdown i..1, a live preview, worked C examples, edge cases, and complexity.
i..1 per row
Row with outer i = 1 prints 1; row with i = 5 prints 54321 — digits grow from the right.
i = 1..rows
for (i = 1; i <= rows; i++) — row width increases from one digit to rows digits.
j = i..1
for (j = i; j >= 1; j--) prints digits in reverse order on each row.
Same line / next line
Digits use printf("%d", j); end each row with printf("\n").
rows = 3..9
Pick row count and draw the reverse row triangle in the browser.
Complexity
Total prints = 1+2+…+n = n(n+1)/2 — a triangular number.
A reverse row number triangle grows digits from the right: each row prints numbers from the current row index down to 1. With rows = 5, you get 1, 21, 321, 4321, 54321.
In C use an outer loop counting up from 1 to rows, an inner loop printing j from i down to 1, then printf("\n") after each row.
It pairs with Program 6’s left-growing triangle — the inner loop counts down instead of up, teaching reverse iteration.
i = 1..rows — narrow row first.
Countdown from i to 1.
Program 6 outer down, inner i..rows; Program 7 outer up, inner i..1.
Follow Program 6; continue to Program 8 next.
In short: outer i = 1..rows, inner j = i..1, printf("%d", j) per digit, then printf("\n").
Given row count rows = 5, print a reverse row number triangle — row outer index i shows digits i..1.
// rows = 5
//1
//21
//321
//4321
//54321 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — also the widest row digit count. |
i (outer) | int | Current row index — runs 1 up to rows. |
j (inner) | int | Prints i..1 with printf("%d", j). |
| Row width | int | Row with outer i prints exactly i digits. |
| First row | int | Single digit 1 when i = 1. |
| Last row | string | Digits rows..1 when i = rows. |
for i from 1 to rows:
for j from i down to 1:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Ascending outer | for (i = 1; i <= rows; i++) | Narrow-first row order |
| Inner i..1 | Countdown from i to 1 | Right-growing triangle |
| User-input rows | scanf | Flexible height |
| Compact trace | rows = 3 on paper first | Quick dry-runs |
| Spaced output | printf("%d ", j) | Readable columns |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Inner loop | for (j = i; j >= 1; j--) printf("%d", j); |
| End row | printf("\n"); |
| Program 6 contrast | Program 6: outer down, inner i..rows; Program 7: outer up, inner i..1 |
Same reverse row triangle — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
scanfRead row count from console
rows = 3Quick dry-run on paper
i = 1..rowsAscending row index
j = i..1Countdown per row
Reach for this pattern when teaching ascending outer loops, inner countdown, and comparing shapes with Program 6.
Natural companion to Program 6 — same triangular print count, inner loop counts down instead of up.
Inner loop j-- from i to 1 — essential countdown practice.
Classic nested-loop question — explain outer up, inner countdown before coding.
Compare this right-growing triangle with the next pattern in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in ascending outers, inner countdown, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the reverse row number triangle in the browser.
Three complete C programs — fixed rows, user input, and a compact trace with rows = 3. Click View Output to reveal sample console results.
Print five rows of the reverse row number triangle with nested loops.
rows = 5Hard-coded height — outer loop up, inner loop counts down each row.
#include <stdio.h>
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = i; j >= 1; j--)
printf("%d", j);
printf("\n");
}
return 0;
} Outer i runs 1 to 5 — inner j prints i down to 1 on each row.
Read row count from the user with validation.
Configurable height with scanf and a positive-rows check.
#include <stdio.h>
int main() {
int rows;
int i, j;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++) {
for (j = i; j >= 1; j--)
printf("%d", j);
printf("\n");
}
return 0;
} Same nested loops — only the row count comes from console input with safe parsing.
Use rows = 3 for a quick paper trace before larger triangles.
rows = 3 TraceSmall triangle — easy to dry-run on paper before scaling up.
#include <stdio.h>
int main() {
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = i; j >= 1; j--)
printf("%d", j);
printf("\n");
}
return 0;
} Three rows, six total digits — trace i and j on paper before coding rows = 5.
int rows = 5; sets how many rows to print.
for (i = 1; i <= rows; i++) moves from row 1 to row 5.
for (j = i; j >= 1; j--) prints digits in reverse order for each row.
printf("\n") moves to the next row after each line is printed.
Total printed digits follow triangular numbers: n(n+1)/2, so time complexity is O(n²).
rows = 5Trace each row — outer i sets width, inner j counts down from i to 1.
| Row (i) | Inner j values | Output line |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2, 1 | 21 |
| 3 | 3, 2, 1 | 321 |
| 4 | 4, 3, 2, 1 | 4321 |
| 5 | 5, 4, 3, 2, 1 | 54321 |
Total digits printed: 1+2+3+4+5 = 15 = 5×6/2 — the fifth triangular number.
Where this tiny pattern (and its countdown inner loop) shows up beyond the homework prompt.
Inner j-- from i to 1 — concrete reverse iteration practice.
Example: trace row 3 and watch j print 3, 2, 1.
Program 6 grows digits from the left; Program 7 grows from the right — same O(n²) total.
Example: print both patterns side by side for rows = 5.
Practice printf vs printf("\n") without complex math.
Example: put printf("\n") inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 55.
Pair the pattern with scanf and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer up and inner countdown first — then write the loops.
Why this pattern earns a permanent spot in beginner C courses.
Using j++ instead of j-- shows up immediately as wrong row order.
Only loops and console output — no arrays or math libraries.
Add spaces, right-align, or swap digits for stars with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row adds one digit on the right.
Small habits that keep reverse-row triangle code clean.
Use rows (or n) and keep i/j for row/column loops.
Avoid crashes when the user types letters instead of a number.
Only call printf("\n") after the inner loop finishes the row.
for (j = i; j >= 1; j--) matches “row i prints digits i..1” naturally.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if rows print ascending digits (12, 123, 1234), you used j++ instead of j--.
Mistakes that commonly break reverse row number triangles.
j++ prints ascending digits per row — 12, 123, 1234 instead of 21, 321, 4321.
→ Use for (j = i; j >= 1; j--).
All digits print on one long line without a row break.
→ Call printf("\n") after the inner loop.
Invalid input may print nothing or behave unexpectedly.
→ Validate rows > 0 before the loops.
Letters or empty input leave rows unread when scanf is unchecked.
→ Check scanf return value and re-prompt on failure.
Each digit prints on its own line — vertical output instead of a triangle.
→ Use printf("%d", j) inside, printf("\n") outside only.
Check these inputs before calling the solution done.
Prints only 1 — inner loop runs once with j = 1.
Outer loop never runs — print nothing or show a message.
Output 1 then 21 — good quick test.
Reject with validation — outer loop condition fails silently otherwise.
Unchecked scanf leaves rows uninitialized — check the return value.
Still O(n²) prints — cap rows for console demos.
Try these variations to lock in the pattern.
printf("%d ", j)rows = 3 before codingi prints exactly i digits — the triangle widens from the right.n(n+1)/2 — a triangular number. For rows = 5, that is 15 digits.1..i ascending; Program 7 prints i..1 descending — mirror per-row logic.rows down to 1 — e.g. 54321 when rows = 5.Quick Takeaway: outer i = 1..rows, inner j = i..1, printf("%d", j), then printf("\n").
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Compact trace (Example 3) | O(rows²) | O(1) |
The reverse row number triangle is a compact nested-loop exercise: outer counts up, inner counts down, and each row grows one digit wider from the right. Master the fixed rows = 5 version, then try user input with scanf and the compact rows = 3 trace.
Practice the three examples above, then continue to Program 8 for the next pattern in the series.
Use j-- for reverse digits per row, keep printf("\n") outside the inner loop, and validate row count when reading from the console.
for (j = i; j >= 1; j--)printf("\n") after each inner looprows > 0 for user inputrows = 3 on paper firstj++ when the pattern needs countdownprintf("\n") inside the inner looprows = 3 dry-run before larger demosPrint the reverse row number triangle the beginner-friendly way.
Row i prints i..1
Definitioni = 1..rows
Loopj = i..1 countdown
Loopprintf then newline
I/OO(n²) time
AnalysisEach row prints digits in reverse order — outer i runs 1..rows, inner j counts down from i to 1 — producing 1, 21, 321, and so on. Total prints grow as O(n²).
Move on to the next pattern in the C number-pattern series.
11 people found this page helpful