Python Repeating Number Triangle Pattern (Descending)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A descending repeating number triangle prints digit i repeated rows - i + 1 times, counting i down from rows.

Remember
Rule: for i = n..1, print i exactly (n - i + 1) times

5
44
333
2222
11111   ← 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 is the ascending twin (1, 22, 333…).

How to Solve It

Outer loop counts down from rows to 1. Inner loop repeats digit i exactly rows - i + 1 times. End each row with print().

MethodIdeaBest for
Nested loopsOuter picks digit n..1; inner repeats it n-i+1 timesLearning, interviews, exams
str(i) * countBuild each full row in one callLeaner demos once loops click

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from rows down to i:
        print i (no newline)
    print newline

Cheat sheet

GoalPattern
Pick digit (count down)for i in range(rows, 0, -1):
Repeat digit ifor j in range(rows, i - 1, -1): print(i, end="")
End the rowprint()
String multiply formprint(str(i) * (rows - i + 1))
Ascending twin (Program 9)for i in range(1, rows + 1): with print(i, end="") repeated i times

Printing Numbers vs Starting a New Line

APIEffectUse for
print(i, end="")Stays on the same lineEach repeat of digit i
print()Ends the current lineAfter the inner loop

Print all repeats without a newline, then end the row once.

Live Preview

Change the row count and the repeating triangle 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 result 5 rows · 15 digits
5
44
333
2222
11111

Worked Walkthrough

Trace rows = 4 — each outer-loop value of i, how many times the inner loop runs, and the printed row.

iInner jRepeatsPrinted row
44..414
34..3233
24..23222
14..141111

Total digits are 1+2+3+4 = 10 — the triangular number n(n+1)/2.

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 repeats it rows - i + 1 times.

Python
rows = 5

for i in range(rows, 0, -1):
    for j in range(rows, i - 1, -1):
        print(i, end="")
    print()

How It Works

1. Count down. for i in range(rows, 0, -1) yields 5, 4, 3, 2, 1 — the digit for each row.

2. Grow repeats. When i = 5, the inner loop runs once (5); when i = 1, it runs five times (11111).

3. Newline. Bare print() after the inner loop starts the next longer row of a smaller 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(rows, 0, -1):
    for j in range(rows, i - 1, -1):
        print(i, end="")
    print()

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 4 and end at 1111.

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(rows, 0, -1):
    print(str(i) * (rows - i + 1))

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 i in range(1, rows + 1) with print(i) × i

That is Program 9 (1, 22, 333). Keep counting down and growing repeats for this pattern.

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.

wrong count

range(1, i + 1) instead of range(rows, i - 1, -1)

The inner bound must run rows - i + 1 times so repeats grow as the digit shrinks.

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.

Time and Space Complexity

ProgramTimeExtra 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 1+2+…+n digits, so total work is n(n+1)/2.

Key Takeaways

  • Rule: for i = n..1, print digit i exactly n - i + 1 times.
  • Inner bound: range(rows, i - 1, -1) grows the repeat count as the digit shrinks.
  • end= vs print(): repeats stay on the line; bare print() advances after each row.
  • Next step: Program 11 inverts the triangle (longest row of the top digit first).

One line: for each digit from n down to 1, print it n-i+1 times with end="", then call print().

Frequently Asked Questions

The outer loop decreases i from rows down to 1. The inner loop runs j from rows down to i, so it executes once when i equals rows, twice when i is rows-1, and so on until rows times for i = 1.
for j in range(rows, i - 1, -1) runs (rows - i + 1) times. That count grows as i shrinks, which is why 4 prints twice, 3 prints three times, and 1 prints rows times.
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 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 (5, 44, 333, 2222, 11111).
O(n²) where n is the number of rows. Total print calls equal 1+2+…+n = 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 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.

Next: Inverted Repeating Triangle

Continue with the longest row of the top digit first.

Program 11 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful