Print letters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the inverted forward triangle updates instantly — including letter and print counts.
Whole numbers from 1 to 26 (A–Z). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · A–E · 15 letters
AAAAA
BBBB
CCC
DD
E
Trace
Worked Walkthrough — rows = 4 (A–D)
Trace each outer letter i and count how many times the inner loop prints it.
i
Inner j
Printed row
Count
A
D..A
AAAA
4
B
D..B
BBB
3
C
D..C
CC
2
D
D..D
D
1
Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. That triangular sum is why time is O(n²).
Code
Python Programs
Three complete programs: fixed A–E, input(), and a ch * repeat shortcut. Use View Output to reveal sample results.
Example 1 — Fixed 'A' up to 'E'
Hard-coded range — ideal for first demos and screenshots.
Python
top = ord('E')
for i in range(ord('A'), top + 1):
for j in range(top, i - 1, -1):
print(chr(i), end="")
print()
Output
AAAAA
BBBB
CCC
DD
E
How It Works
1. Outer loop picks the letter.i runs from ord('A') to ord('E') — that letter fills the whole row.
2. Inner loop sets the width.j runs from top down to i, so widths are 5, 4, 3, 2, 1.
3. Print i, not j.print(chr(i), end="") keeps the row uniform (AAAAA, not EDCBA).
4. Break the line. Bare print() after the inner loop starts the next row.
Example 2 — User Input Version
Read the height at runtime. Prefer try/except ValueError and clamp to 1–26.
Python
raw = input("Enter the number of rows: ").strip()
try:
rows = int(raw)
except ValueError:
print("Please enter a whole number of rows >= 1.")
else:
if rows < 1:
print("Please enter a whole number of rows >= 1.")
else:
if rows > 26:
rows = 26
for row in range(rows):
ch = chr(ord('A') + row)
repeat = rows - row
for k in range(repeat):
print(ch, end="")
print()
Output (when user enters 4)
Enter the number of rows: 4
AAAA
BBB
CC
D
How It Works
1. Prompt and parse. Ask for a row count, then convert with int(...) inside try/except ValueError.
2. Index form of the same shape. Row 0 prints Arows times; row 1 prints Brows - 1 times; and so on.
3. Clamp for A–Z. Cap at 26 so ord('A') + row never walks past Z.
Example 3 — ch * repeat
Build each repeated-letter row in one call — same shape, no explicit inner print loop.
Python
rows = 5
for row in range(rows):
ch = chr(ord('A') + row)
repeat = rows - row
print(ch * repeat)
Output
AAAAA
BBBB
CCC
DD
E
How It Works
1. One outer loop. Still walk row from 0 to rows - 1.
2. Build the row.ch * repeat creates a string of length repeat filled with ch.
3. Print and advance.print prints that string and ends the line.
Learn the two-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
print(j)
Stepping letters
If you print chr(j) instead of chr(i), the first row becomes EDCBA. Always print the outer letter for this shape.
Countdown outer
Program 11 by mistake
Counting i from top down to A prints EEEEE first. Outer loop must advance A→top.
print early
Column of letters
If bare print() (or default print(chr(i))) sits inside the inner loop, each letter lands on its own line. Use end="" for letters; call print() only after the inner loop.
rows > 26
Past Z
Clamp to 1–26 so ord('A') + row never walks past Z.
rows = 1
Single A
Output is just A — a good sanity check.
Bad input
Catch ValueError
Bare int(input()) raises on letters — wrap in try/except ValueError and require 1–26.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
ch * repeat (Example 3)
O(rows²)
O(rows) per temporary row string
Total letters printed = n + (n-1) + … + 1 = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: letters advance A→top; widths shrink n..1.
Print i: the outer letter fills the row; j only sets the count.
Break the row:end="" for letters; bare print() after the inner loop.
Complexity:O(n²) time from the triangular letter count; O(1) extra space for nested loops.
One line: for each letter i from A to top, print i(top - i + 1) times, then print().
Frequently Asked Questions
Program 11 prints EEEEE, DDDD, ... (letters step down). Program 12 prints AAAAA, BBBB, ... (letters step up) with the same inverted widths 5..1.
When i is A, the inner loop runs from E down to A (5 times) and prints A each time. Next i becomes B, the inner loop runs 4 times (E..B) and prints B.
The inner loop only controls how many times to print. Printing the outer letter keeps the entire row the same letter; printing the inner counter would step letters across the row.
Because the inner loop runs from the fixed top letter down to the current i. As i increases, the loop has fewer iterations.
print(ch, end="") stays on the same line. print() ends the current line. Letters use end=""; the row break uses print() after the inner loop.
O(n²) where n is the number of rows. Total printed characters equal n+(n-1)+…+1 = n(n+1)/2.
Yes. print(ch * repeat) prints a full repeated-letter row in one call. Nested loops are better for learning; ch * repeat is a handy shortcut later.
Use a try/except ValueError around int(input()), or check raw.isdigit() before converting, then clamp rows between 1 and 26 so bad input does not walk past Z.
🤔
Did you know?
This is the forward-letter twin of Program 11: same inverted widths (5…1), but letters advance A→E instead of stepping down. Print the outer loop letter inside the inner loop so each row stays uniform.