Python Number Pattern (Reverse Growing)

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

What Is This Pattern?

A reverse left-growing number triangle always starts each row at the peak digit rows, then counts down farther each time — 5, 54, 543, and so on.

Remember
Rule: for i from rows down to 1
        for j from rows down to i: print j (no space)

5
54
543
5432
54321     ← rows = 5

Follows the reverse-row triangle in Program 7; next is the repeating number triangle in Program 9.

How to Solve It

Outer loop counts down from rows to 1. Inner loop prints digits rows..i. End each row with a bare print().

MethodIdeaBest for
Descending outer + descending innerOuter i = rows..1; inner j = rows..i; print jLearning, interviews, exams
Spaced digitsprint(str(j) + " ", end="") instead of print(j, end="")Easier reading on each row

Pseudocode

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

Cheat sheet

GoalPattern
Pick each rowfor i in range(rows, 0, -1):
Print digitsfor j in range(rows, i - 1, -1): print(j, end="")
End of rowprint()
Spaced formprint(str(j) + " ", end="")
vs Program 6 / 76: i..rows up; 7: i..1; 8: rows..i down

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="")Stays on the same lineEach digit on the row
print()Ends the current lineAfter the inner loop finishes

Digits use end=""; a bare print() ends the row once.

Live Preview

Change the row count and the reverse left-growing 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 result rows = 5 · 15 digits
5
54
543
5432
54321

Worked Walkthrough — rows = 5

Trace how every row starts at 5 while the stopping digit i moves leftward.

iInner jPrints
55..55
45..454
35..3543
15..154321

Total digits = n(n+1)/2 → O(n²). Same outer loop as Program 6; opposite inner direction.

Python Programs

Three complete programs: fixed rows = 5, user-input rows, 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 rows..i with no spaces.

Python
rows = 5

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

How It Works

1. Outer down. i starts at 5 and moves toward 1 — that extends how far the countdown goes.

2. Inner from peak. range(rows, i - 1, -1) always starts at rows, so every row begins with the same digit.

3. Newline. Call bare print() only after the inner loop finishes the row.

Example 2 — User Input Rows

Read rows safely, validate, then run the same nested loops.

Python
try:
    rows = int(input("Enter the number of rows: "))
except ValueError:
    print("Please enter a positive integer.")
    raise SystemExit

if rows <= 0:
    print("Please enter a positive integer.")
    raise SystemExit

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

How It Works

1. Prompt and validate. Catch ValueError; reject non-positive values before looping.

2. Same core. Outer down / inner rows..i matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable single-digit output:

Safer input
try:
    rows = int(input("Enter the number of rows: "))
except ValueError:
    print("Enter a whole number from 1 to 9.")
    raise SystemExit

if rows < 1 or rows > 9:
    print("Enter a whole number from 1 to 9.")
    raise SystemExit

Example 3 — Compact rows = 3

Same structure with only three rows — easy to trace on paper.

Python
rows = 3

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

How It Works

1. Three rows. i = 3 → 3; i = 2 → 32; i = 1 → 321.

2. Trace on paper. Confirm every row starts with 3 — only the end digit changes.

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.

j = i..rows

Got Program 6 instead

If the inner loop counts up from i to rows, you get 5, 45, 345… Keep range(rows, i - 1, -1).

i = 1..rows

Wrong outer direction

An ascending outer with inner i..1 is Program 7. Program 8 needs outer rows..1 and inner rows..i.

print inside

Broken rows

If bare print() 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 print(str(j) + " ", end="").

ValueError

Validate input

Use try/except ValueError and require rows > 0 before looping — bad input should not crash.

Time and Space Complexity

ProgramTimeExtra 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.

Key Takeaways

  • Always start at peak: outer rows..1, inner rows..i.
  • Same first digit: every row begins with rows; only the end digit moves left.
  • Break the row: print(j, end="") in the inner loop; bare print() after it.
  • Complexity: O(n²) time from n(n+1)/2 digits; O(1) extra space.

One line: count i down from rows, print j from rows down to i, then newline.

Frequently Asked Questions

A reverse left-growing triangle: row 1 shows 5, row 2 shows 54, row 3 shows 543, and so on until the last row shows digits from rows down to 1.
The inner loop always starts at j = rows. Only the stopping value i changes, so each row begins with the peak digit.
When i = 1, the inner loop prints j from rows down to 1.
Program 7 outer counts up and prints i..1 (1, 21, 321). Program 8 outer counts down and inner always starts at rows (5, 54, 543).
Program 6 inner prints i..rows ascending (5, 45, 345). Program 8 inner prints rows..i descending (5, 54, 543).
print(j, end="") stays on the same line. print() ends the row after the inner loop.
Change rows or read it from user input with int(input()) inside try/except — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.

Did you know?

Each row starts at rows and counts down to i — outer i runs rows..1, inner j prints rows..i — producing 5, 54, 543, and so on. Total prints grow as O(n²).

Next: Repeating Number Triangle

Continue with rows that repeat the row index (1, 22, 333).

Program 9 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