#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
}
Output
12345
1234
123
12
1
How It Works
1. Outer sets the length.i starts at 5 and decreases to 1.
2. Inner prints digits.j always runs from 1 to i on that row.
3. Newline. Call cout << "\n" only after the inner loop finishes the row.
Example 2 — User Input Rows
Read rows with cin, validate, then run the same nested loops.
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter the number of rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1234
123
12
1
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Outer count-down / inner 1..i matches Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable single-digit 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 the count-down outer loop.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
}
Output
123
12
1
How It Works
1. Three rows.i = 3 → 123; i = 2 → 12; i = 1 → 1.
2. Trace on paper. If the outer loop counted up instead, you would get 1, 12, 123 — the ascending variant.
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 = 1..rows
Ascending by mistake
If the outer loop counts up, you get 1, 12, 123… Keep i = rows..1 for this pattern.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.
j = i..rows
Got Program 2 instead
Starting the inner loop at i produces 12345, 2345, 345… — that is Program 2.
rows = 1
Single digit
Output is just 1 — a good sanity check for input validation.
rows > 9
Multi-digit width
Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced cout << j << " ".
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)
Total digits printed = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Count down: outer i = rows..1 makes the first row the longest.
Always start at 1: inner j = 1..i rebuilds each row from the left.
Break the row:cout << j in the inner loop; cout << "\n" after it.
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 1..i, then newline.
Frequently Asked Questions
A descending number triangle: the first row prints 1..rows, the next prints one fewer digit, down to a single 1 — for example 12345, 1234, 123, 12, 1.
Counting down makes the first row the longest. for (i = rows; i >= 1; i--) sets i to the full width first, then shrinks by one each line.
Digits 1 through i. for (j = 1; j <= i; j++) cout << j; builds each row from left to right.
Change the outer loop to for (i = 1; i <= rows; i++). Keep the inner loop as for (j = 1; j <= i; j++) cout << j.
cout << j stays on the same line. cout << "\n" ends the current line. Digits use cout << j; the row break uses cout << "\n" after the inner loop.
Program 1 always starts each row at 1. Program 2 starts at i and prints through rows (12345, 2345, 345…).
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are 1+2+…+n = n(n+1)/2.
🤔
Did you know?
Row i prints digits 1 through i. The outer loop counts down from rows, so the first line is longest and each row shortens by one digit — still O(n²) total prints.