A reverse repeating alphabet triangle grows by one repeated letter on each new line while letters count downward from the top of the alphabet range.
Remember
Rule: for letter $i from top down to 'A',
print $i with growing width 1, 2, 3, …
E
DD
CCC
BBBB
AAAAA ← 5 rows (top = 'E')
It is the reverse of Program 9: same growing widths, but letters step E → A instead of A → E. Print the outer letter inside the inner loop so each row stays uniform. Flip the widths in Program 11 to get the inverted twin.
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; print 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 $top down to 'A':
for $j from $top down to $i:
echo $i
echo PHP_EOL
1. Outer loop picks the letter.$i runs from 'E' down to 'A'.
2. Inner loop sets the width.$j runs from 'E' down to $i — once for E, twice for D, and so on.
3. Print the outer letter.echo $i keeps the whole row the same character.
4. Break the line.echo PHP_EOL after the inner loop starts the next row.
Example 2 — User Input Version
Compute $top from the row count, then grow $repeat from 1 as letters count down. Prefer validating with is_numeric in real apps.
PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));
$top = chr(ord('A') + $rows - 1);
$repeat = 1;
for ($ch = $top; $ch >= 'A'; $ch--) {
for ($k = 1; $k <= $repeat; $k++) {
echo $ch;
}
echo PHP_EOL;
$repeat++;
}
Output (when user enters 4)
Enter the number of rows: 4
D
CC
BBB
AAAA
How It Works
1. Prompt and read. Ask for a row count, then cast the trimmed line to an int.
2. Compute the top letter. For $rows = 4, $top is 'D' — first row is D.
3. Grow the repeat count. Start at $repeat = 1 and increment after each row so widths become 1, 2, 3, 4.
4. Safer input tip. Prefer:
Safer input
$raw = trim(fgets(STDIN));
if (!is_numeric($raw)) {
echo "Enter a whole number from 1 to 26." . PHP_EOL;
exit;
}
$rows = (int) $raw;
if ($rows < 1 || $rows > 26) {
echo "Enter a whole number from 1 to 26." . PHP_EOL;
exit;
}
Example 3 — str_repeat($ch, $repeat)
Build each repeated-letter row in one call — same shape, no explicit character loop.
1. Same countdown. Still walk $ch from $top down to 'A'.
2. Build the row.str_repeat($ch, $repeat) creates a string of length $repeat filled with $ch.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
echo $j
Stepping letters on a row
If you print $j instead of $i, letters change across the row. Print the outer letter to keep each row uniform.
$i++
Forward triangle by mistake
Counting letters upward reprints Program 9. Keep $i-- from the top letter down to 'A'.
PHP_EOL inside
Column of letters
If echo PHP_EOL sits inside the inner loop, each letter lands on its own line. Break the row only after the inner loop.
$rows = 1
Single A
Output is just A on one line — a good sanity check.
$rows > 26
Past Z
chr(ord('A') + $rows - 1) leaves A–Z when $rows > 26. Clamp or reject in interactive programs.
Bad input
Use is_numeric
Bare (int) fgets(STDIN) turns letters into 0 — validate before the outer loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
str_repeat (Example 3)
O(rows²)
O(rows) per temporary row string
Total letters = 1 + 2 + … + n = n(n + 1)/2 — still quadratic in n.
Remember
Key Takeaways
Rule: letters count down; widths grow 1, 2, 3, …
Uniform row: print the outer letter ($i), not the inner counter.
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 from $top down to 'A', print that letter with a growing width, then echo PHP_EOL.
Frequently Asked Questions
Program 9 uses A, BB, CCC, ... (letters increase). Program 10 uses E, DD, CCC, ... (letters decrease) with the same growing repeat counts.
On the 4th row the outer-loop letter is B, and the inner loop runs four times, printing B each time.
$j only controls how many times the loop runs. Printing $i keeps the row the same letter; printing $j would change letters across the row.
With for ($j = $top; $j >= $i; $j--), when $i is near the top the range is short; as $i moves toward A the range lengthens, so repeat counts grow 1, 2, 3, …
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²) where n is the number of rows. Total echo calls equal 1+2+…+n = n(n+1)/2.
Yes. echo str_repeat($ch, $repeat) . PHP_EOL prints a full repeated-letter row in one call. Nested loops are better for learning; str_repeat is a handy shortcut later.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, then clamp rows between 1 and 26 so bad input does not walk past A.
🤔
Did you know?
This pattern is the reverse of Program 9: row widths still grow 1, 2, 3, …, but letters run backward (E, then D, then C, …). Print the outer loop letter inside the inner loop so each row stays uniform.