Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — outer shrinks rows..1, inner prints i..1.
C++
#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;
}
Output
54321
4321
321
21
1
How It Works
1. Outer shrinks the start.i begins at rows and decreases — the longest row prints first.
2. Inner counts down. Print j from i down to 1 so each row reads in reverse.
3. Newline once. Call cout << "\n" only after the inner loop finishes.
Example 2 — User Input Rows
Read rows with cin, validate, then use the same two-loop core.
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = rows; i >= 1; i--)
{
for (j = i; j >= 1; j--)
cout << j;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter rows: 4
4321
321
21
1
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Descending outer + descending inner matches Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm both loops count downward.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = i; j >= 1; j--)
cout << j;
cout << "\n";
}
return 0;
}
Output
321
21
1
How It Works
1. Three rows.i = 3 → 321; i = 2 → 21; i = 1 → 1.
2. Trace on paper. If the outer loop counts up instead of down, the shortest row prints first.
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++
Growing outer loop
Using for (i = 1; i <= rows; i++) prints the shortest row first. Keep i = rows..1.
j++
Ascending inner loop
for (j = 1; j <= i; j++) prints 12345 style rows — not reverse. Use j = i..1.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, you get one digit per line. Call it only after the inner loop.
j = rows
Full line every row
Starting j at rows every time reprints 54321 on each line. Start at i.
rows = 1
Single digit
Output is just 1. A good sanity check for input validation.
cin
Check the stream
Validate cin >> rows before looping — a failed read leaves rows unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Row starting at i prints i digits. Total = n + (n−1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Two descending loops: outer rows..1, inner i..1.
Longest first: start the outer loop at rows so the first row is the widest.
Break the row: digits in the inner loop; cout << "\n" after it finishes.
Complexity:O(n²) time from n(n+1)/2 digits; O(1) extra space.
One line: for each i from rows down to 1, print i..1, then newline.
Frequently Asked Questions
A reverse descending triangle: for rows=5 you get 54321 / 4321 / 321 / 21 / 1 — each row prints i down to 1 while the outer loop shrinks.
The inner loop runs j = i down to 1, so each row prints i, i-1, ..., 1.
Because rows = 5 and the outer loop starts with i = rows. The first row prints from 5 down to 1.
Program 2 prints i..rows (left-shifted). Program 3 prints i..1 (reverse descending) with a shrinking outer loop.
Use cout << j << " " instead of cout << j in the inner loop.
cout << j prints digits on the same line. cout << "\n" ends the row after the inner loop finishes.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows. Total prints are n + (n-1) + … + 1 = n(n+1)/2.
🤔
Did you know?
This 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.