A fill-with-n triangle prints a fixed-width row each time — first the ascending sequence i..n, then pad the rest with n.
Remember
Rule: for i = n..1
print i..n, then pad with n (i - 1 times)
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5 ← n = 5
After Program 18’s odd/even triangle, this pattern teaches two inner loops that keep every row the same width.
Approach
How to Solve It
Walk i from n down to 1; print the sequence, then pad with n.
Method
Idea
Best for
Two inner loops
Sequence i..n; fill i - 1 times with n
Learning, interviews
input() width
Same loops; read n at runtime
Interactive practice
Custom fill
Same shape; pad with a separate constant
Fill variants
Pseudocode
Pseudocode
for i from n down to 1:
for j from i to n:
print j (same line, spaced)
for j from 1 to i - 1:
print n (same line, spaced)
print newline
Cheat sheet
Goal
Pattern
Set width
n = 5
Outer loop
for i in range(n, 0, -1):
Sequence
for j in range(i, n + 1): print(j, end=" ")
Fill pad
for j in range(1, i): print(n, end=" ")
End row
print()
Width per row
Always n
Custom fill
print(fill, end=" ") in the second loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(j, end=" ")
Stays on the same line
Each number
print()
Ends the current line
After both inner loops
Print numbers with end=" ", then break once with print(). Putting print() inside an inner loop prints one number per line.
Try it
Live Preview
Change n and the fill-with-n triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · values = 25
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
Trace
Worked Walkthrough — n = 3
Trace each outer i, the sequence segment, the fill count, and the printed row.
i
Sequence i..n
Fill count
Printed row
3
3
2 × 3
3 3 3
2
2 3
1 × 3
2 3 3
1
1 2 3
none
1 2 3
Fill count = i - 1. Every row has exactly n values. Total = n².
Code
Python Programs
Three complete programs: fixed n = 5, input() width, and custom fill. Use View Output for sample results.
Example 1 — Fixed n = 5
Hard-coded width — sequence loop, then pad with n.
Python
n = 5
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end=" ")
for j in range(1, i):
print(n, end=" ")
print()
Output
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
How It Works
1. Outer loop.i runs from 5 down to 1 — the start of the ascending sequence.
2. Sequence. Print j from i to n.
3. Fill. Print n exactly i - 1 times, then print() ends the row.
Example 2 — input() Width
Read n at runtime; fill value matches the width.
Python
n = int(input("Enter the triangle width: "))
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end=" ")
for j in range(1, i):
print(n, end=" ")
print()
2. Same shape. Sequence and fill still use the same n — only the source changes.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the triangle width: ").strip()
try:
n = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if n < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Example 3 — Custom Fill
Pad with fill = 9 while the sequence still runs up to n = 5.
Python
n = 5
fill = 9
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end=" ")
for j in range(1, i):
print(fill, end=" ")
print()
Output
5 9 9 9 9
4 5 9 9 9
3 4 5 9 9
2 3 4 5 9
1 2 3 4 5
How It Works
1. Same structure. Outer and both inner loop bounds match Example 1.
2. Only the pad changes. The second loop prints fill instead of n.
3. Sequence unchanged. The ascending i..n part still uses the width n.
Edge Cases & Pitfalls
Check these before calling the solution done.
missing fill
Uneven rows
Without the second loop, early rows are short. Always pad i - 1 times.
print() inside
Vertical numbers
If print() is inside either inner loop, each number lands on its own line.
ascending outer
Upside-down order
Using range(1, n + 1) for the outer loop prints the full sequence first. Keep range(n, 0, -1) for this shape.
range(1, i + 1)
One extra pad
Fill with range(1, i) — that is i - 1 times. range(1, i + 1) overshoots the width.
n = 1
Single value
Output is just 1 — the fill loop never runs.
input()
Catch ValueError
Letters crash bare int(input()) — wrap in try/except and require n >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(1)
Each of n rows prints n values → total n² → O(n²).
Remember
Key Takeaways
Rule: print i..n, then pad with n for i - 1 slots.
Fixed width: every row has exactly n values.
end= vs print(): numbers stay on the line; print() advances after both loops.
Complexity: total values = n² → O(n²).
One line: print the rising tail, then fill the rest of the row with n.
Frequently Asked Questions
When i = 5, the sequence loop prints j = 5 once, then the fill loop runs 4 times — all values are 5, so the row is 5 5 5 5 5.
The first prints the increasing sequence i..n. The second fills remaining positions with n so every row has the same width.
When i = 1, the sequence loop prints j = 1 to 5 and the fill loop runs zero times — no padding needed.
print(j, end=" ") stays on the same line with a trailing space. print() ends the current line. Numbers use end=" "; the row break uses print() after both inner loops.
Replace the hard-coded n in the fill loop with any constant — see Example 3 with fill = 9.
Rows will have different widths — the top row may be short while the bottom row is full length.
O(n²) for width n because each of n rows prints n numbers.
Use try/except ValueError around int(input()) so bad input does not crash the script.
Only one row prints — a single 1.
🤔
Did you know?
Each row prints an ascending sequence i..n, then pads with n so every row has width n. The second inner loop runs i - 1 times — still O(n²) total prints.