An inverted repeating alphabet triangle prints a wide first row of one letter, then shrinks by one repeated letter on each new line while letters count down.
Remember
Rule: for letter $i from top down to 'A',
print $i exactly (ord($i) - ord('A') + 1) times
EEEEE
DDDD
CCC
BB
A ← 5 rows (top = 'E')
It is the flip of Program 10: same countdown letters, but widths shrink 5, 4, 3, …, 1 instead of growing. Print the outer letter inside the inner loop so each row stays uniform.
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 = 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 'A' 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 'A' to $i — five times for E, four 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 shrink with $repeat = ord($ch) - ord('A') + 1. 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);
for ($ch = $top; $ch >= 'A'; $ch--) {
$repeat = ord($ch) - ord('A') + 1;
for ($k = 1; $k <= $repeat; $k++) {
echo $ch;
}
echo PHP_EOL;
}
Output (when user enters 4)
Enter the number of rows: 4
DDDD
CCC
BB
A
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 DDDD.
3. Same shrink core.$repeat = ord($ch) - ord('A') + 1 gives widths 4, 3, 2, 1 as $ch counts down.
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.
Flip of Program 10: same letters — widths shrink instead of grow.
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 for every $j from 'A' to $i, then echo PHP_EOL.
Frequently Asked Questions
Program 10 prints widths 1..5 (E, DD, CCC, ...). Program 11 prints widths 5..1 (EEEEE, DDDD, ...). Both use repeating letters per row and letters counting down.
When $i is E, the inner loop runs from A through E (5 times) and prints E each time. Next $i becomes D, the inner loop runs 4 times (A..D) and prints D.
$j only controls how many times the loop runs. Printing $i keeps the entire row the same letter; printing $j would step letters across the row.
Because the inner loop stops at $j <= $i. Smaller $i means fewer iterations, so fewer characters are printed.
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 n+(n-1)+…+1 = 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 Z.
🤔
Did you know?
This is the inverted twin of Program 10: letters still step E→A, but widths shrink 5, 4, 3, …, 1 instead of growing. Print the outer loop letter inside the inner loop so each row stays uniform.