Shape Rule
Wide first, then shrink
Row 1 prints EEEEE, then DDDD, down to a single A.

First row: five Es. Each following row is one character shorter and uses the next letter down: EEEEE, DDDD, CCC, BB, A. Compare with Program 10 (width grows) and worked PHP examples, live preview, edge cases, and complexity.
Wide first, then shrink
Row 1 prints EEEEE, then DDDD, down to a single A.
Letter countdown
for ($i = 'E'; $i >= 'A'; $i--) picks the letter for each row.
Width 5…1
for ($j = 'A'; $j <= $i; $j++) shrinks as i falls — print $i, not $j.
Same line / next line
Letters use echo; end each row with echo PHP_EOL;.
1–26 rows
Pick a row count and draw the inverted repeating triangle in the browser.
Complexity
Total letters = n(n+1)/2; extra memory stays O(1).
An inverted repeating alphabet triangle starts wide and shrinks by one repeated letter on each new line, counting letters downward from the top of the alphabet range. With the right angle on the left, the console shows an upside-down staircase of identical letters per row.
In PHP you usually solve it with two nested for loops: the outer loop picks the row letter (counting down), the inner loop prints that same letter fewer times each row, then echo PHP_EOL moves to the next line.
It shows that “inverted” often means flipping only the width bound — not inventing a new algorithm. Once outer letter vs inner count clicks, Program 10, 11, and 12 are one-line cousins.
Top row repeats the highest letter n times.
Rows go 5, 4, 3, …, 1 while letters go E→A.
echo $i; in the inner loop; echo PHP_EOL; after.
Same letters — opposite width direction.
In short: for each letter i from top down to A, print i repeatedly (ord($i) - ord('A') + 1) times, then call echo PHP_EOL.
Given a positive integer rows (or a fixed top letter like 'E'), print a left-aligned inverted triangle where each row repeats one letter and widths shrink from rows down to 1.
// First 5 rows (conceptual shape)
// EEEEE
// DDDD
// CCC
// BB
// A | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines (typically 1–26). Top letter = chr(ord('A') + $rows - 1). |
| Printed output | text | Left-aligned rows; letter ch is repeated ord($ch) - ord('A') + 1 times. |
$top = chr(ord('A') + $rows - 1)
for ch from top down to 'A':
$repeat = ord($ch) - ord('A') + 1
for k from 1 to repeat:
print ch (no newline)
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested char loops | Outer letter + inner A..i width | Learning and interviews |
str_repeat($ch, $repeat) | Build a whole row in one call | Shorter production-style demos |
| Goal | Pattern |
|---|---|
| Walk letters downward | for ($i = 'E'; $i >= 'A'; $i--) |
| Shrink repeat count | for ($j = 'A'; $j <= $i; $j++) |
| Print row letter | echo $i; — not $j |
| End the row | echo PHP_EOL; |
| One-line row shortcut | echo str_repeat($ch, $repeat) . PHP_EOL; |
| Growing widths | See Program 10 (E, DD, CCC, …) |
Same triangle — different ways to emit characters.
same linePrints a letter without moving to the next line
new lineEnds the current row after all repeats are printed
whole rowBuilds $n copies of $ch at once — skip the inner loop
echo $iMaster printing the outer letter before the string shortcut
Reach for this triangle when practicing inverted widths with repeating letters.
Flip only the width direction while keeping letter countdown.
j <= i from A naturally shrinks as i falls.
Use $repeat = ord($ch) - ord('A') + 1 instead of a growing formula.
Next: same shrink, but letters advance A→E.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that proves inversion is usually a bound change — letter choice and width can flip independently.
Choose a row count between 1 and 26 and draw the inverted repeating alphabet triangle in the browser.
Three complete PHP programs — fixed top letter, CLI input, and a str_repeat shortcut. Click View Output to reveal sample console results.
Print five inverted rows with classic nested char loops.
'E' down to 'A'Hard-coded range — ideal for first demos and screenshots.
<?php
for ($i = 'E'; $i >= 'A'; $i--) {
for ($j = 'A'; $j <= $i; $j++) {
echo $i;
}
echo PHP_EOL;
} When i = 'E', the inner loop runs from A to E (5 times) and prints E. When i = 'D', it prints DDDD, and so on until a single A. Printing i (not j) keeps each row uniform.
Let the user choose the height at runtime.
Compute $top = chr(ord('A') + $rows - 1), then shrink with $repeat = ord($ch) - ord('A') + 1. Check is_numeric() in real apps.
<?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;
} For rows = 4, top is 'D'. Letter D repeats 4 times, C three times, and so on. Clamp rows to 1–26 so top stays within A–Z.
Same shape without an explicit inner print loop.
str_repeat($ch, $repeat)Build each repeated-letter row in one call, then print it.
<?php
$rows = 5;
$top = chr(ord('A') + $rows - 1);
for ($ch = $top; $ch >= 'A'; $ch--) {
$repeat = ord($ch) - ord('A') + 1;
echo str_repeat($ch, $repeat) . PHP_EOL;
} str_repeat($ch, $repeat) creates a string of length $repeat filled with $ch. Great once you understand the nested-loop idea; keep the two-loop version for exams that ask you to show both bounds.
Use fgets(STDIN) when reading input. Fix the top letter or compute it from $rows.
for ($i = 'E'; $i >= 'A'; $i--) selects the character printed on the row.
for ($j = 'A'; $j <= $i; $j++) runs 5, 4, 3… times; print $i with echo $i.
echo PHP_EOL ends the row so the next outer iteration starts fresh.
Total letters: n+(n-1)+…+1 = n(n+1)/2 — O(n²) time, O(1) extra memory.
'E' down to 'A'Trace each outer-loop value of i and count how many times the inner loop runs.
i | Inner j range | Printed row | Repeats |
|---|---|---|---|
'E' | 'A'..'E' | EEEEE | 5 |
'D' | 'A'..'D' | DDDD | 4 |
'C' | 'A'..'C' | CCC | 3 |
'B' | 'A'..'B' | BB | 2 |
'A' | 'A'..'A' | A | 1 |
Total letter prints: 5 + 4 + 3 + 2 + 1 = 15 = 5×6/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest demo that j <= i from A shrinks as i falls.
Example: compare with Program 10’s growing inner bound.
Teach grow vs shrink as a one-bound change.
Example: side-by-side E/DD/CCC vs EEEEE/DDDD/CCC.
Practice ord($ch) - ord('A') + 1 for shrinking widths.
Example: ord('D') - ord('A') + 1 = 4.
Swap to lowercase or mix digits once the loops work.
Example: start from chr(ord('a') + $rows - 1).
Descending triangular totals still make O(n²) concrete.
Example: 5+4+…+1 = 15 for n = 5.
Pair the pattern with is_numeric() and 1–26 clamps.
Example: reject rows <= 0 or rows > 26.
Pro Tip: say “outer picks the letter, inner shrinks the width” before coding — that story prevents mixing Program 10’s growing formula here.
Why this pattern earns a spot right after the growing reverse triangle.
Wrong width formula shows up immediately as a growing instead of shrinking shape.
Only loops, chars, and console output — no arrays required.
Flip to Program 10 by growing the repeat count instead.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the nested-loop version first; treat str_repeat($ch, $repeat) as a polish shortcut afterward.
Small habits that keep alphabet-pattern code clean.
Use ch for the row letter and $repeat = ord($ch) - ord('A') + 1 for the shrinking count.
is_numeric()Avoid crashes when the user types letters instead of a number.
Only call echo PHP_EOL; after the inner loop finishes the row.
For A–Z demos, reject or clamp rows > 26.
Trace rows = 3 (CCC, BB, A) on paper before coding larger demos.
Pro Tip: if you get E, DD, CCC instead of EEEEE, DDDD, CCC, you reused Program 10’s growing repeat formula.
Mistakes that commonly break inverted repeating alphabet patterns.
j Instead of iRows become A…E sequences instead of repeated letters.
→ Always echo $i (or $ch) for this shape.
$repeat = (ord($top) - ord($ch)) + 1 grows widths — wrong for this page.
→ Use $repeat = ord($ch) - ord('A') + 1 (or j from A to i).
Each letter lands on its own line — you get a column, not a triangle.
→ Use echo for letters; echo PHP_EOL only after the inner loop.
(int) CastLetters or empty input need validation — cast carefully.
→ Check is_numeric() and re-prompt on failure.
chr(ord('A') + $rows - 1) can leave the A–Z range.
→ Clamp to 26 or define wrap/error behavior explicitly.
Check these inputs before calling the solution done.
Output is just A on one line.
Treat as invalid; re-prompt instead of silent empty output.
rows < 0Invalid height — validate before computing top.
Clamp or error — char math leaves A–Z.
Non-numeric input becomes 0 — check is_numeric() first.
Same loops work with 'a' and chr(ord('a') + $rows - 1).
Try these variations to lock in the pattern.
is_numeric() until 1 <= rows <= 26chr(ord('a') + $rows - 1) as the top lettern(n+1)/2 — hence O(n²) time.1 <= rows <= 26 for interactive A–Z programs.Quick Takeaway: outer loop picks the letter (counting down), inner loop shrinks the width, then break the line — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
str_repeat($ch, $repeat) (Example 3) | O(rows²) | O(rows) per row string (temporary) |
The inverted repeating alphabet triangle is a small nested-loop exercise with lasting payoff: outer letter vs shrinking width, char countdown, and O(n²) intuition. Master the classic two-loop version, then optionally shorten rows with str_repeat($ch, $repeat).
Practice the three examples above, then compare with Program 10 or continue to Program 12’s forward-letter invert.
Print the outer letter with echo, end rows with echo PHP_EOL, and use ord($ch) - ord('A') + 1 (not Program 10’s growing formula) for the width.
echo for letters and echo PHP_EOL after each row1 <= rows <= 26 for interactive programsis_numeric() after fgets(STDIN)repeat formula hereecho PHP_EOL inside the inner letter looprows > 26 without a clear policyPrint the inverted repeating triangle the beginner-friendly way.
Letters down, width down
DefinitionPicks the row letter
CodeShrinks with echo $i;
Ends each row
I/OO(n²) time
AnalysisThis 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.
Keep the shrinking widths, but advance letters forward: AAAAA, BBBB, CCC, …
12 people found this page helpful