C++ Repeating Number Triangle Pattern (Descending)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A descending repeating number triangle prints digit i with a growing repeat count as i counts down — 5, 44, 333, 2222, 11111.
Remember
Rule: for i from rows down to 1
repeat print i exactly (rows - i + 1) times
5
44
333
2222
11111 ← rows = 5
Follows the ascending repeating triangle in Program 9; next is the inverted repeating triangle in Program 11.
Approach
How to Solve It
Outer loop picks digit i (counting down). Inner loop runs rows - i + 1 times and prints i each time.
Method
Idea
Best for
Count-down outer + bound inner
Outer i = rows..1; inner j = rows..i; print i
Learning, interviews, exams
Counted repeats
Inner j = 1..(rows - i + 1); print i
Same shape, clearer repeat formula
Pseudocode
Pseudocode
for i from rows down to 1:
for j from rows down to i:
print i
print newline
Cheat sheet
Goal
Pattern
Pick each digit
for (i = rows; i >= 1; i--)
Repeat digit
for (j = rows; j >= i; j--) cout << i;
End of row
cout << "\n";
Repeat count
rows - i + 1 (grows as i shrinks)
vs Program 9
9: i times of i ascending; 10: growing repeats while digit falls
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << i
Stays on the same line
Each repeated digit on the row
cout << "\n"
Ends the current line
After the inner loop finishes
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the descending 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
5
44
333
2222
11111
Trace
Worked Walkthrough — rows = 5
Trace how the digit shrinks while the repeat count grows.
i
Inner runs
Prints
5
1 time
5
4
2 times
44
3
3 times
333
1
5 times
11111
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 picks digit i; inner controls how many times it repeats.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = rows; j >= i; j--)
cout << i;
cout << "\n";
}
return 0;
}
Output
5
44
333
2222
11111
How It Works
1. Outer picks the digit.i starts at 5 and decreases to 1.
2. Inner sets repeats.j runs from rows down to i — that is rows - i + 1 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 = rows; j >= i; j--)
cout << i;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
4
33
222
1111
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 digit vs repeat count.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = rows; j >= i; j--)
cout << i;
cout << "\n";
}
return 0;
}
Output
3
22
111
How It Works
1. Three rows.i = 3 → 3; i = 2 → 22; i = 1 → 111.
2. Trace on paper. If you use Program 9’s ascending outer loop instead, you get 1, 22, 333 — not this shape.
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.
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.
i = 1..rows
Got Program 9 instead
An ascending outer loop with j = 1..i yields 1, 22, 333… Keep outer rows..1 here.
\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.
rows > 9
Multi-digit width
Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced cout << i << " ".
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 picks a shrinking digit.
Growing repeats: inner runs rows - i + 1 times and always prints i.
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 rows - i + 1 times, then newline.
Frequently Asked Questions
A descending repeating triangle: for rows=5 you get 5, 44, 333, 2222, 11111 — digit shrinks each row while the repeat count grows.
The outer loop decreases i from rows to 1. The inner loop runs j from rows down to i, so it executes once when i equals rows, and rows times when i equals 1.
for (j = rows; j >= i; j--) runs (rows - i + 1) times. That count grows as i shrinks.
Program 9 grows upward: row i repeats digit i exactly i times (1, 22, 333). Program 10 starts with one copy of the top digit and increases repeats as the digit decreases.
Program 11 is inverted: widest row first (55555, 4444, 333…). Program 10 starts narrow and grows (5, 44, 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 1+2+…+n = n(n+1)/2.
🤔
Did you know?
Row digit i repeats rows - i + 1 times. The outer loop counts down from rows, so the first row shows one copy of the top digit and the last row repeats 1 for rows times — still O(n²) total prints.