A repeating number triangle prints the row digit on every column of that row — so you see 1, 22, 333, and so on.
Remember
Rule: outer i = 1..rows; inner j = 1..i; print i
1
22
333
4444
55555 ← rows = 5
Unlike Program 8 (descending digits from rows) or Program 5 (ascending 1..i), this pattern keeps the print value fixed at i for the whole row.
Approach
How to Solve It
Outer loop from 1 to rows. Inner loop from 1 to current i, printing i each time. End the line after the inner loop.
Method
Idea
Best for
Nested loops
Outer i, inner prints i × i times
Learning, interviews, demos
Compact trace
Same logic with rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print i
print newline
Cheat sheet
Goal
Pattern
Outer (row digit)
for (i = 1; i <= rows; i++)
Inner (repeat count)
for (j = 1; j <= i; j++)
Print digit
cout << i;
End the row
cout << "\n";
Digits on row i
i (same digit, i times)
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << i
Stays on the same line
Each digit
cout << "\n"
Ends the line
After the inner loop
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the repeating triangle updates instantly.
Use rows from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
1
22
333
4444
55555
Trace
Worked Walkthrough
Trace three outer values when rows = 5 — watch the digit and the repeat count both equal i.
Outer i
Inner j
Prints
1
1..1
1
3
1..3
333
5
1..5
55555
The inner loop counts columns; the printed value is always the outer i, not 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 i from 1 to 5, inner prints i that many times.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
cout << i;
cout << "\n";
}
return 0;
}
Output
1
22
333
4444
55555
How It Works
1. Outer sets the digit.i is both the row index and the value you print.
2. Inner sets the count.j runs from 1 to i, so the digit appears i times.
3. End the row. Call cout << "\n" only after the inner loop finishes.
Example 2 — User Input Rows
Read the row count at runtime with a simple validation tip.
C++
#include <iostream>
using namespace std;
int main()
{
int rows, i, j;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
cout << i;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1
22
333
4444
How It Works
1. Same loop core. Only the source of rows changes from a literal to cin.
2. Entering 4 stops early. You get four rows ending at 4444.
3. Validate in real apps. Prefer checking cin failure and requiring a positive height (tip below).
Safer input tip
if (!(cin >> rows) || rows < 1)
{
cout << "Please enter a positive integer.\n";
return 1;
}
Example 3 — Compact rows = 3
Smaller height for a quick paper trace of digit and repeat count.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
cout << i;
cout << "\n";
}
return 0;
}
Output
1
22
333
How It Works
1. Only three rows. Easy to dry-run every i and j value on paper.
2. Same formula. Nothing changes except rows — proving the pattern scales.
3. Print i, not j. Row 2 prints 22 — two copies of 2, not 12.
Edge Cases & Pitfalls
Check these before calling the solution done.
Print value
Print i, not j
Printing j reprints Program 5 (1, 12, 123), not this shape.
Inner bound
Inner must run to i
Stopping at rows makes every row the same length and breaks the triangle.
Newlines
Do not put "\n" inside the inner loop
That prints one digit per line and destroys the triangle.
Bad cin
Validate input
Check cin >> rows and require rows >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Triangle (Examples 1–3)
O(n²)
O(1)
Digits printed are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n. Extra memory is only the loop variables.
Remember
Key Takeaways
Rule: outer i = 1..rows, inner j = 1..i, print i.
Growth: each row adds one more copy of a larger digit.
Break the row: call cout << "\n" only after the inner loop finishes.
Complexity:O(n²) time, O(1) extra space.
One line: print the row digit i exactly i times, then move to the next line.
Frequently Asked Questions
A repeating number triangle: for rows=5 you get 1, 22, 333, 4444, 55555.
The inner loop prints the current row number i every time. It runs i times, so i appears i times on that row.
When i = 4, the inner loop runs 4 times and prints 4 each time — giving 4444.
Program 8 prints descending digits rows..i (5, 54, 543). Program 9 repeats the row digit i times (1, 22, 333).
Program 5 prints 1..i ascending. Program 9 uses the same inner bound 1..i but prints i on every iteration.
Printing a digit stays on the same line. Printing a newline ends the current row. Digits use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
One row prints a single digit 1.
🤔
Did you know?
Each row repeats the row number i exactly i times — outer i runs 1..rows, inner j prints i on every pass — producing 1, 22, 333, and so on. Total prints grow as O(n²).