A right-aligned alphabet pyramid prints growing prefixes of the alphabet (A, A B, A B C, …) pushed right with leading spaces so every row shares the same right edge.
Remember
Rule: for row letter i from 'A' to top,
print (top - i) spaces, then A..i (width 2)
A
A B
A B C
A B C D
A B C D E ← top = 'E'
Unlike Program 22, letters restart from A on every row. Unlike Program 16, this only right-aligns — it does not fully center the pyramid.
Approach
How to Solve It
Two ways to emit the same shape — char loops with pads, then optionally rewrite with int row indexes.
Method
Idea
Best for
Char loops
Pad $top..$i+1; print A..$i with %2s
Learning, interviews, exams
Int row index
Pad $n - $row; print $row letters from A
When you think in row numbers
Pseudocode
Pseudocode
for $i from 'A' to $top:
for $j from $top down to ($i + 1):
echo " "
for $k from 'A' to $i:
printf "%2s" with $k
echo PHP_EOL
Cheat sheet
Goal
Pattern
Walk each row
for ($i = 'A'; $i <= $top; $i++)
Leading spaces
for ($j = $top; $j > $i; $j--) echo " ";
Prefix letters
for ($k = 'A'; $k <= $i; $k++) printf("%2s", $k);
End the row
echo PHP_EOL;
Pad count
ord($top) - ord($i) (4 when $i is A and $top is E)
1. Derive height.$n = ord($top) - ord('A') + 1 is 5 when $top is 'E'.
2. Pad by row number. Row 1 pads $n - 1 spaces; the last row pads 0.
3. Letters from A. Letter $L is chr(ord('A') + $L) — row 1 prints A; row 5 prints A..E.
Edge Cases & Pitfalls
Check these before calling the solution done.
k++
Continuous stream by mistake
If you keep a running $k++ across rows, you get Program 22-style output. Restart the letter loop at 'A' each row.
No pads
Left-aligned instead
Skipping the space loop prints a left-aligned triangle. Keep $j from $top down while $j > $i.
PHP_EOL inside
Broken outline
If echo PHP_EOL is inside either inner loop, each cell lands on its own line. Use echo/printf for pads and letters; end the row only after both loops.
top = A
Single letter
Output is just a width-2 A cell. Pad loop never runs. A good sanity check.
top < A
Empty output
Outer loop never runs if $top < 'A'. Validate A–Z before looping.
Bad input
Validate one letter
Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.
Analysis
Time and Space Complexity
Program
Time
Extra space
Char loops (Examples 1–2)
O(n²)
O(1)
Int row index (Example 3)
O(n²)
O(1)
For $n = ord($top) - ord('A') + 1 rows, each row prints up to O(n) spaces plus O(n) letters. Letter total = 1 + 2 + … + n = n(n + 1)/2.
Remember
Key Takeaways
Rule: for row $i, print ord($top) - ord($i) spaces, then letters A..$i.
Restart A: every row begins the letter loop at 'A' — not a running $k++.
Break the row: call echo PHP_EOL only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row letter $i from A to $top, print ord($top) - ord($i) spaces, then A..$i with width 2, then echo PHP_EOL.
Frequently Asked Questions
The first inner loop prints (ord('E') - ord($i)) leading spaces, fewer on each lower row, so the letter block ends at the same right edge.
Width-2 formatting prints each letter in an even column so the output matches the spaced layout (best viewed in a monospace font).
Program 22 uses a continuous k++ stream across all rows. Program 27 restarts letters from A on every row and uses leading spaces to push rows to the right.
Remove the leading-space loop and print letters directly from A to the row letter.
O(n²) for n rows because each row prints O(n) spaces plus O(n) letters.
Use trim(fgets(STDIN)), take the first character after strtoupper, require A–Z, and reject empty or multi-character tokens.
Program 16 centers the pyramid with more padding. This pattern only right-aligns by shrinking leading spaces while restarting A..i each row.
Yes. Pad n − row spaces, then print row letters as chr(ord('A') + $L). Example 3 on this page shows that style.
🤔
Did you know?
Leading padding: for each row letter i, the loop prints ord('E') - ord($i) spaces. Then the letter loop prints A through i using %2s so columns look even in monospace output. The last row has no padding; all rows share the same right edge.