A reverse alphabet decreasing triangle prints each row backward down to A, while the starting letter moves one step earlier each row — so the left edge slides inward.
Remember
Rule: for start letter $i from last down to A,
print $i down through A
EDCBA
DCBA
CBA
BA
A ← 5 rows (every row ends at A)
Same widths as Program 6 (5, 4, 3, 2, 1), but Program 6 prints forward to a fixed end (ABCDE, BCDE, …). Here both loops decrement. Compare with Program 8, which keeps E fixed on the left and shortens the tail.
Approach
How to Solve It
Two ways to emit the same shape — start with nested letter loops, then optionally reverse a prefix once and slice.
Method
Idea
Best for
Nested letter loops
Outer = lowering start; inner = start..A downward
Learning, interviews, exams
strrev + substr($top, $i)
Build EDCBA… once, drop one left letter each row
Shorter demos once loops click
Pseudocode
Pseudocode
for $start from $lastLetter down to 'A':
for $ch from $start down to 'A':
echo $ch
echo PHP_EOL
1. Outer loop picks the start letter.$i runs from 'E' down to 'A' — longest row first.
2. Inner loop walks down to A. For each start, $j runs from $i down to 'A', so the row is $i..A in reverse.
3. Print letters, then break the line.echo $j stays on the row; echo PHP_EOL after the inner loop starts the next (shorter) row.
When $i = 'E' you get EDCBA; when $i = 'A' you get A.
Example 2 — User Input Version
Read the row count at runtime. Prefer validating with is_numeric and clamp to 26 (shown in the tip below).
PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));
$top = chr(ord('A') + $rows - 1);
for ($i = $top; $i >= 'A'; $i--) {
for ($j = $i; $j >= 'A'; $j--) {
echo $j;
}
echo PHP_EOL;
}
Output (when user enters 4)
Enter the number of rows: 4
DCBA
CBA
BA
A
How It Works
1. Prompt and read. Ask for a row count, then cast the trimmed line to an int.
2. Map rows to a top letter.$top = chr(ord('A') + $rows - 1) — for $rows = 4, $top is 'D'.
3. Same reverse-shrink core. Only the source of $top changes — the print logic matches Example 1.
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 — strrev + substr($top, $i)
Build the first reverse row once, then drop one left letter each time — same shape, no nested letter loop.
1. Take and reverse the prefix.ABCDE reversed with strrev becomes EDCBA — the first printed row.
2. Slice from index $i.substr($top, 0) is the full row; substr($top, 1) drops E; and so on down to A.
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.
$j++
Program 6 by mistake
If the inner loop increments instead of decrements, you print forward suffixes. Use for ($j = $i; $j >= 'A'; $j--).
Fixed start
Program 8 shape
If the outer loop stays on 'E' and only shortens the stop, you get EDCBA, EDCB, … Advance (lower) the start with $i--.
PHP_EOL inside
Column of letters
If echo PHP_EOL is inside the inner loop, each letter lands on its own line. Use echo for letters; break the row only after the inner loop.
$rows > 26
Past Z
chr(ord('A') + $rows - 1) leaves A–Z when $rows > 26. Clamp or reject in interactive programs.
$rows = 1
Single A
Output is just A — start and end coincide. A good sanity check.
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)
strrev + substr (Example 3)
O(rows²)
O(rows) for the reversed prefix and temporary row strings
Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — quadratic in n. Same totals as Programs 1 and 6.
Remember
Key Takeaways
Rule: lower start letter $i; print $i down through A each row.
vs Program 6: same widths — here letters run backward to A; there they run forward to a fixed end.
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 $i from the top letter down to 'A', print $i down through A, then echo PHP_EOL.
Frequently Asked Questions
The outer loop picks the starting letter (E, then D, then C…). The inner loop prints from that start down to A. Each row drops the previous leftmost letter and becomes shorter.
The outer loop decreases the starting letter each row, and the inner loop decreases letters within that row down to A. That matches the reverse-alphabet output.
Because the inner loop condition includes $j == 'A', so the final character printed on each row is always A.
Program 6 prints forward suffixes ending at E (ABCDE, BCDE, …). This pattern prints reverse runs ending at A (EDCBA, DCBA, …). Same shrinking widths; opposite letter direction.
Program 8 always starts each row at E and shortens the tail (EDCBA, EDCB, EDC). Program 7 changes the first letter each row (E, then D, then C…).
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. Reverse the A…top prefix with strrev, then echo substr($top, $i) . PHP_EOL while $i advances. Nested letter loops are better for learning; strrev 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 Z.
🤔
Did you know?
Both loops run backward: the outer loop lowers the start letter; the inner loop prints down to A. For 5 rows: EDCBA, DCBA, CBA, BA, A. Same widths as Program 6, opposite letter direction.