Shape Rule
i..1 per row
Row 1 prints 54321, row 2 prints 4321, shrinking until a single 1.

The reverse descending number triangle prints 54321, 4321, 321, 21, 1 — a natural step after the left-shifted triangle in Program 2. This tutorial covers descending outer and inner loops, a live preview, algorithm steps, worked C++ examples, edge cases, and complexity.
i..1 per row
Row 1 prints 54321, row 2 prints 4321, shrinking until a single 1.
rows..1
for (i = rows; i >= 1; i--) shrinks the row length each iteration.
i..1 descending
for (j = i; j >= 1; j--) prints digits in reverse order on each row.
Same line / next line
Digits use cout << j; end each row with cout << "\n".
3–9 rows
Pick a row count and draw the reverse descending triangle in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
A reverse descending number triangle prints each row from i down to 1 while the outer loop shrinks the row length. With rows = 5, the output is 54321, 4321, 321, 21, 1.
In C++ the outer loop runs i = rows..1, the inner loop prints j from i down to 1, then cout << "\n" moves to the next line.
It teaches descending inner loops — a key step after Program 2’s left-shifted ascending rows.
Outer loop i = rows..1 shortens each row.
Inner loop j = i..1 counts downward.
Program 2 ascends i..rows; Program 3 descends i..1.
Follow Program 2; continue to Program 4 (left-aligned descending) next.
In short: for each i from rows down to 1, print j from i down to 1, then cout << "\n".
Given a positive integer rows (e.g. 5), print a reverse descending triangle: each row i shows digits from i down to 1, with the outer loop counting from rows down to 1.
// rows = 5 (conceptual shape)
// 54321
// 4321
// 321
// 21
// 1 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines — outer loop runs from rows down to 1. |
i | int | Outer loop — current row limit; also the first digit printed. |
j | int | Inner loop — descending from i down to 1. |
for i from rows down to 1:
for j from i down to 1:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 54321, 4321, … | Learning and interviews |
| User-input rows | cin >> rows; | Flexible console programs |
| Spaced output | cout << j << " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = rows; i >= 1; i--) |
| Print digits i..1 | for (j = i; j >= 1; j--) cout << j; |
| End the row | cout << "\n"; |
| Spaced digits | cout << j << " "; |
| User input | cin >> rows; |
| Program 2 contrast | for (i = 1; i <= rows; i++) with j = i..rows |
Same reverse descending triangle — different ways to control rows and formatting.
i = rows..1Shrinks row length each line
j = i..1Descending digits per row
i = rowsLongest row on top
j--Inner loop must count down
Reach for this pattern when teaching descending inner loops and shrinking row lengths.
Natural follow-up after Program 2 — introduces a descending inner loop.
Outer/inner bound practice with an immediate visual check.
Combine loops with cin for a flexible row count.
Compare Program 2 (left-shifted) and Program 4 (left-aligned descending) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the reverse descending triangle in the browser.
Three complete C++ programs — fixed rows, user input, and spaced output variant. Click View Output to reveal sample console results.
Print five rows of the reverse descending triangle with nested descending loops.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = rows; i >= 1; --i) {
for (j = i; j >= 1; --j)
cout << j;
cout << "\n";
}
return 0;
} When i = 5, the inner loop prints 5, 4, 3, 2, 1 — output 54321. When i = 1, only one digit prints — output 1. The outer loop shrinks i each row.
Read the row count with cin instead of hard-coding 5.
Read rows with cin >> rows (check cin.fail() in real apps); both loops use rows as the starting bound.
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = rows; i >= 1; --i) {
for (j = i; j >= 1; --j)
cout << j;
cout << "\n";
}
return 0;
} Same descending-loop core as Example 1; only the source of rows changes from a literal to user input. Non-numeric input sets cin’s fail bit if you ignore validation — always check cin.fail() in safer labs.
Add a space between digits for easier reading on each row.
Keep rows = 5 but print each digit followed by a space.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = rows; i >= 1; --i) {
for (j = i; j >= 1; --j)
cout << j << " ";
cout << "\n";
}
return 0;
} Only the print statement changes — cout << j << " " instead of cout << j. Loop bounds stay the same as Example 1.
#include <iostream> brings in cout and cin. Set loop variables i, j with rows = 5.
for (i = rows; i >= 1; i--) — descending outer loop shrinks each row.
for (j = i; j >= 1; j--) — prints digits i..1 in reverse order.
cout << "\n" ends the row after the inner loop finishes.
Rows shrink from rows digits to one — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i, the inner-loop range, digit count, and full row output.
i | Inner loop (j) | Prints | Row output |
|---|---|---|---|
5 | 5, 4, 3, 2, 1 | 5 | 54321 |
4 | 4, 3, 2, 1 | 4 | 4321 |
3 | 3, 2, 1 | 3 | 321 |
2 | 2, 1 | 2 | 21 |
1 | 1 | 1 | 1 |
Prints per row = i — total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: flip j-- to j++ and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 4 for a left-aligned descending triangle.
Practice cout vs row newline without complex math.
Example: put cout << "\n" inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use cout << j << " " between digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for rows = 5 — total is 15 (5+4+3+2+1).
Pair the pattern with cin.fail() checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C++ courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character 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 shortens by one digit.
Small habits that keep number-pattern code clean.
Outer loop counts down (i--); inner loop must also count down from i to 1.
cinCheck cin.fail() so bad input does not leave rows unset.
Only call cout << "\n" after the inner loop finishes the row.
for (j = i; j >= 1; j--) prints digits i..1 in reverse order.
Trace i = 3, 2, 1 on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put cout << "\n" inside the inner loop.
Mistakes that commonly break reverse descending number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use cout << j for digits; cout << "\n" only after the inner loop.
for (j = 1; j <= i; j++) prints ascending digits — you get Program 1’s shape, not this one.
→ Keep for (j = i; j >= 1; j--) so each row reads i..1.
for (i = 1; i <= rows; i++) grows rows instead of shrinking them.
→ Use for (i = rows; i >= 1; i--) so the first row is the longest.
j = rows on every row prints the same full line repeatedly.
→ Start the inner loop at the current outer value: j = i.
Letters or empty input leave rows unset.
→ Check cin.fail() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 21 and 1.
Unchecked cin sets the fail bit — check cin.fail() before using rows.
Each row prints i digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
rows each rowcout << j << " " between digitsi = rows..1. Inner loop: j = i..1 with j--.cout stays on the line; cout << "\n" advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.i prints exactly i digits — compare with Program 2 where each row prints rows - i + 1 digits.Quick Takeaway: outer loop i = rows..1, inner loop j = i..1 with cout << j, then cout << "\n".
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The reverse descending number triangle is a compact nested-loop lesson: a descending outer loop shrinks each row while the inner loop prints digits from i down to 1. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 4 for the left-aligned descending number triangle.
Row i prints i..1 — keep cout << j for digits and cout << "\n" for the break, and validate row counts when reading input.
for (i = rows; i >= 1; i--) in the outer loopfor (j = i; j >= 1; j--) prints digits in reversecout << j for digits and cout << "\n" after each rowrows ≥ 1 for interactive programscin.fail() before using rowscout << "\n" inside the inner digit looprows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints i..1
DefinitionCounts down rows
Codej = i down to 1
CodeEnds each row
ShapeO(n²) time
AnalysisThis pattern prints each row in descending order from i down to 1. The outer loop shrinks the row length while the inner loop counts downward — producing 54321, 4321, 321, and so on.
Move on to the left-aligned descending number triangle in the C++ number-pattern series.
12 people found this page helpful