Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the inverted repeating triangle updates instantly — capped at 9 for readable single-digit demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
55555
4444
333
22
1
Trace
Worked Walkthrough — rows = 5
Trace how the digit and the repeat count both shrink together.
i
Inner runs
Prints
5
5 times
55555
4
4 times
4444
3
3 times
333
1
1 time
1
Total digits = n(n+1)/2 → O(n²). Always print i, never j.
Code
C++ Programs
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 counts down; inner prints i exactly i times.
C++
#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 << i;
cout << "\n";
}
return 0;
}
Output
55555
4444
333
22
1
How It Works
1. Outer picks the digit.i starts at 5 and decreases to 1.
2. Inner matches the count.j runs from 1 to i — that is exactly i prints of i.
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 << i;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
4444
333
22
1
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Outer count-down / inner print-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 widest-first repeating digits.
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 << i;
cout << "\n";
}
return 0;
}
Output
333
22
1
How It Works
1. Three rows.i = 3 → 333; i = 2 → 22; i = 1 → 1.
2. Trace on paper. If the outer loop counted up instead, you would get Program 9 (1, 22, 333).
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
Got Program 9 instead
An ascending outer loop yields 1, 22, 333… Keep outer rows..1 for the inverted shape.
j = rows..i
Got Program 10 instead
Using for (j = rows; j >= i; j--) grows repeats as the digit falls (5, 44, 333…). Keep j = 1..i here.
print j
Wrong values on the row
If you print j instead of i, you get changing digits per position. Always print the outer variable.
\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.
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)
Total digits printed = n + (n-1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Widest first: outer i = rows..1 starts with the longest row.
Print i, i times: inner j = 1..i — same bound as Program 9, opposite outer direction.
Break the row:cout << i 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 i exactly i times, then newline.
Frequently Asked Questions
An inverted repeating triangle: for rows=5 you get 55555, 4444, 333, 22, 1 — widest row first, then each line shortens.
The outer loop decreases i from rows to 1, and the inner loop runs j from 1 to i. Row i repeats digit i exactly i times.
for (j = 1; j <= i; j++) cout << i runs the inner body i times. When i is 5 you get 55555; when i is 1 you get a single 1.
Program 10 grows repeats as the digit shrinks (5, 44, 333…). Program 11 repeats digit i exactly i times (55555, 4444, 333…).
Program 9 uses an ascending outer loop (1, 22, 333…). Program 11 uses the same inner bound but counts the outer loop down (55555, 4444, 333…).
cout << i stays on the same line. cout << "\n" ends the current line. Repeated digits use cout << i; the row break uses cout << "\n" after the inner loop.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are n+(n-1)+…+1 = n(n+1)/2.
🤔
Did you know?
Row digit i repeats exactly i times. The outer loop counts down from rows, so the first row is widest (rows copies of rows) and each line shortens — still O(n²) total prints.