A shrinking repeating number pattern prints digit i repeated rows - i + 1 times, counting i up from 1.
Remember
Rule: for i = 1..n, print i exactly (n - i + 1) times
11111
2222
333
44
5 ← 5 rows
In Python you solve it with two nested for loops: the outer loop picks the digit, the inner loop repeats it with print(i, end=""), then bare print() ends the row. Program 9 grows instead (1, 22, 333…); Program 11 inverts while counting down (55555, 4444, 333…).
Approach
How to Solve It
Outer loop counts up from 1 to rows. Inner loop repeats digit i exactly rows - i + 1 times. End each row with print().
Method
Idea
Best for
Nested loops
Outer picks digit 1..n; inner runs i..n so the row shrinks
Learning, interviews, exams
str(i) * count
Build each full row in one call
Leaner demos once loops click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i to rows:
print i (no newline)
print newline
Cheat sheet
Goal
Pattern
Pick digit (count up)
for i in range(1, rows + 1):
Repeat digit i
for j in range(i, rows + 1): print(i, end="")
End the row
print()
String multiply form
print(str(i) * (rows - i + 1))
Growing twin (Program 9)
for j in range(1, i + 1): print(i, end="")
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(i, end="")
Stays on the same line
Each repeat of digit i
print()
Ends the current line
After the inner loop
Print all repeats without a newline, then end the row once.
Try it
Live Preview
Change the row count and the shrinking pattern updates instantly — capped at 9 so every digit stays a single character.
Whole numbers from 1 to 9 (single digits). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
11111
2222
333
44
5
Trace
Worked Walkthrough
Trace rows = 4 — each outer-loop value of i, how many times the inner loop runs, and the printed row.
i
Inner j
Repeats
Printed row
1
1..4
4
1111
2
2..4
3
222
3
3..4
2
33
4
4..4
1
4
Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.
Code
Python Programs
Three complete programs: fixed height, input(), and a string-multiply shortcut. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop picks the digit; inner loop starts at i so the row shrinks as the digit rises.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(i, rows + 1):
print(i, end="")
print()
Output
11111
2222
333
44
5
How It Works
1. Count up.for i in range(1, rows + 1) yields 1, 2, 3, 4, 5 — the digit for each row.
2. Shrink the row. When i = 1, the inner loop runs five times (11111); when i = 5, it runs once (5).
3. Newline. Bare print() after the inner loop starts the next shorter row of a larger digit.
Example 2 — User Input
Read the row count from the keyboard. Prefer a try / except in real apps (shown in the tip below).
Python
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(i, rows + 1):
print(i, end="")
print()
Output (when user enters 4)
Enter the number of rows: 4
1111
222
33
4
How It Works
1. Read rows.input() returns a string; int(...) converts it to a whole number.
2. Same nested loops. Four rows start at 1111 and end at 4.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the number of rows: ").strip()
try:
rows = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Example 3 — String Multiply Shortcut
Same shape with one call per row — best once the rows - i + 1 count is clear.
Python
rows = 5
for i in range(1, rows + 1):
print(str(i) * (rows - i + 1))
Output
11111
2222
333
44
5
How It Works
1. Count still matters.rows - i + 1 is the same repeat length the inner loop used.
2. Same shape. Identical output to Example 1 — keep the nested-loop version for exams that expect two loops.
3. Convert first.str(i) * count needs a string — multiplying an int would raise an error.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong twin
for j in range(1, i + 1)
That is Program 9 (1, 22, 333). Keep the inner start at i so the row shrinks.
print() inside
Bare print(i) inside the inner loop
That puts each digit on its own line. Use print(i, end="") for repeats; call bare print() only after the row finishes.
count down
for i in range(rows, 0, -1) with print(i) × i
That is Program 11 (55555, 4444, 333). Keep counting up for this pattern.
rows = 1
Single digit
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
rows > 9
print(10, end="") prints two characters. Labs usually stick to 1–9 for a clean single-digit look.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(n²)
O(1)
String multiply (Example 3)
O(n²)
O(1) beyond the row string
There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = 1..n, print digit i exactly n - i + 1 times.
Inner bound:range(i, rows + 1) shrinks the row as the digit rises.
end= vs print(): repeats stay on the line; bare print() advances after each row.
Next step: Program 13 mixes ascending and descending digits across alternating rows.
One line: for each digit from 1 to n, print it n-i+1 times with end="", then call print().
Frequently Asked Questions
The inner loop runs from j = i to rows. As i increases (1, 2, 3, …), the inner loop runs fewer times, so each row prints fewer copies of the row digit.
On the first row, i = 1 and the inner loop runs from 1 to rows, so it prints 1 exactly rows times — five 1s when rows = 5.
print(i, end="") stays on the same line. print() ends the current line. Repeated digits use end=""; the row break uses print() after the inner loop.
Program 11 counts the outer loop down and repeats digit i exactly i times (55555, 4444, 333, 22, 1). Program 12 counts up and uses for j in range(i, rows + 1) so digit i repeats rows - i + 1 times (11111, 2222, 333, 44, 5).
Program 10 counts the digit down while repeats grow (5, 44, 333). Program 12 counts the digit up while the row still shrinks (11111, 2222, 333).
O(n²) where n is the number of rows. Total print calls equal n+(n-1)+…+1 = n(n+1)/2.
Yes. print(str(i) * (rows - i + 1)) prints a full row in one call. Nested loops are better for learning; str(i) * count is a handy shortcut once the bounds make sense.
Wrap int(input()) in try/except ValueError, or check that the raw string is digits, and require rows >= 1.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Row digit i repeats rows − i + 1 times. The outer loop counts up from 1, while the inner loop runs from i to rows, so each row shrinks — still O(n²) total prints.