A repeating alphabet triangle grows by one character each row, but every character on that row is the same letter — the letter advances with the row.
Remember
Rule: for letter i from A to last,
print i with growing width 1, 2, 3, …
A
BB
CCC
DDDD
EEEEE ← 5 rows
Unlike Program 1 (A, AB, ABC), letters do not step across the row — you print the outer loop letter inside the inner loop. The reverse twin is Program 10 (E, DD, CCC, …).
Approach
How to Solve It
Two ways to emit the same shape — start with nested letter loops, then optionally shorten with str_repeat.
Method
Idea
Best for
Nested letter loops
Outer = letter; inner = growing width; echo outer letter
Learning, interviews, exams
str_repeat($ch, $n)
Build a whole repeated-letter row in one call
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 'A' to lastLetter:
for j from 'A' to i:
print i (no newline)
print newline
1. One outer loop. Still walk $row from 1 to $rows and map it to a letter.
2. Build the row.str_repeat($ch, $row) creates a string of length $row filled with $ch.
3. Print and advance. Append PHP_EOL to print that string and end the line.
Learn the two-loop version first (Examples 1–2) so you can explain both bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
echo $j
Program 1 by mistake
If you print $j instead of $i, letters step across the row (A, AB, ABC…). Always print the outer-loop letter.
PHP_EOL early
Column of letters
If echo PHP_EOL is inside the inner loop, each letter lands on its own line. Use echo $i for letters; end the row only after the inner loop.
Wrong width
Rectangle, not triangle
Inner bound must grow with the row ($j <= $i or $col <= $row). A fixed inner bound prints a filled rectangle.
rows = 1
Single A
Output is just A on one line — a good sanity check.
rows > 26
Beyond Z
Cap or reject — the row letter leaves the alphabet. Keep $rows in 1…26 for A–Z demos.
Bad input
Use is_numeric
Bare (int) trim(fgets(STDIN)) turns letters into 0. Prefer is_numeric and clamp to 1…26.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(n²)
O(1)
str_repeat (Example 3)
O(n²)
O(n) per temporary row string
Total letters printed = 1 + 2 + … + n = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: one letter per row; width grows 1, 2, 3, …
Print the outer letter: the inner loop only counts — echo $i, not $j.
Break the row: call echo PHP_EOL only after the inner loop.
Complexity:O(n²) time; O(1) extra space for nested loops.
One line: for each letter $i, echo $i a growing number of times, then echo PHP_EOL.
Frequently Asked Questions
Because the inner loop prints the outer-loop character ($i) every time. The inner counter only controls how many times to print, not which character to print.
Then letters would change across the row (A, AB, ABC…), which is Program 1 — not a repeating-letter triangle.
On the third row the row letter is C, and the inner loop runs three times, printing C each time.
Program 1 prints stepping letters across each row (A, AB, ABC). This pattern keeps one letter per row and only grows the repeat count (A, BB, CCC).
Program 10 uses the same growing widths but letters count downward (E, DD, CCC…). This pattern counts upward (A, BB, CCC…).
echo stays on the same line. echo PHP_EOL ends the current line. Letters use echo; the row break uses echo PHP_EOL after the inner loop.
O(n²) for n rows, because total printed characters are n(n+1)/2.
Yes. echo str_repeat($ch, $row) . PHP_EOL prints a full repeated-letter row in one call. Nested loops are better for learning; str_repeat is a handy shortcut later.
🤔
Did you know?
Each row prints the same letter repeatedly: row 1 prints A once, row 2 prints B twice, row 3 prints C three times. Print the outer loop letter inside the inner loop so the row stays uniform. The reverse twin is Program 10.