PHP Alphabet Triangle Pattern (Odd Length)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An odd-length alphabet triangle reprints A..end on every row, while end jumps by two letters each time — so widths are always 1, 3, 5, 7, …

Remember
Rule: end letter steps A, C, E, …; each row prints A..end

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI     ← 5 rows (ends A,C,E,G,I)

Unlike Program 13 (one running cursor across the whole triangle), every row here restarts at A and grows to a farther odd-step ending letter.

How to Solve It

Step the end letter by two, then print the fresh prefix A..end on each row.

MethodIdeaBest for
Char step +2Outer $i walks A, C, E, …; inner prints A..$iLearning, interviews, fixed demos
Row index$end = chr(ord('A') + 2*($row-1))When the user enters a row count

Pseudocode

Pseudocode
for $i from 'A' to $lastEnd step 2:
    for $j from 'A' to $i:
        echo $j (no newline)
    echo PHP_EOL

Cheat sheet

GoalPattern
Step the end letterfor ($i = 'A'; $i <= 'I'; $i = chr(ord($i) + 2))
Print the prefixfor ($j = 'A'; $j <= $i; $j++) echo $j;
End the rowecho PHP_EOL;
Row-count form$end = chr(ord('A') + 2 * ($row - 1));
PHP step tipUse chr(ord($i) + 2) — do not write $i += 2 on a letter
A–Y-safe heightMax 13 rows ($end reaches Y)

Printing Letters vs Starting a New Line

APIEffectUse for
echoStays on the same lineEach letter on the row
echo PHP_EOLEnds the current lineAfter the inner loop

Print characters without a newline, then end the row once.

Live Preview

Change the row count and the odd-length triangle updates instantly — including end letter and letter totals.

Whole numbers from 1 to 13. Row r ends at letter A + 2*(r-1) (A–Y).

Live result 5 rows · ends I · 25 letters
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI

Worked Walkthrough — $rows = 4

Trace each end letter and the prefix printed from A through that end.

RowEnd $iInner $jPrinted rowCount
1AA..AA1
2CA..CABC3
3EA..EABCDE5
4GA..GABCDEFG7

Total letters: 1 + 3 + 5 + 7 = 16 = 4². That square sum is why time is O(r²).

PHP Programs

Three complete programs: fixed through I, ending-letter input, and a row-count form. Use View Output to reveal sample results.

Example 1 — Fixed through 'I'

Hard-coded ending letter — ideal for first demos and screenshots.

PHP
<?php
for ($i = 'A'; $i <= 'I'; $i = chr(ord($i) + 2)) {
    for ($j = 'A'; $j <= $i; $j++) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Outer loop steps the end. $i takes A, C, E, G, I via $i = chr(ord($i) + 2).

2. Inner loop restarts at A. For each $i, print every letter from A through $i.

3. Break the line. echo PHP_EOL; after the inner loop starts the next longer prefix.

In PHP, step letters with chr(ord($i) + 2) — do not use $i += 2, which coerces the letter to an integer.

Example 2 — Ending Letter Input

Read an odd-step ending letter (A, C, E, …). Prefer validating a single A–Z character in real apps.

PHP
<?php
echo "Enter the ending letter (odd step like I): ";
$end = strtoupper(trim(fgets(STDIN)))[0];

for ($i = 'A'; $i <= $end; $i = chr(ord($i) + 2)) {
    for ($j = 'A'; $j <= $i; $j++) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Prompt and read. Take the first character of the trimmed line into $end.

2. Same nested-loop core. Only the outer upper bound changes — the echo logic matches Example 1.

3. Safer input tip. Prefer a single uppercase letter and an odd step from A:

Safer input
$raw = strtoupper(trim(fgets(STDIN)));
if (strlen($raw) !== 1 || $raw < 'A' || $raw > 'Z'
        || ((ord($raw) - ord('A')) % 2) !== 0) {
    echo "Enter one odd-step letter A, C, E, …, Y." . PHP_EOL;
    exit;
}
$end = $raw;

Example 3 — $end = chr(ord('A') + 2*($row-1))

Drive the pattern from a row count instead of an ending letter.

PHP
<?php
$rows = 5;

for ($row = 1; $row <= $rows; $row++) {
    $end = chr(ord('A') + 2 * ($row - 1));
    for ($j = 'A'; $j <= $end; $j++) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Map row → end. Row 1 ends at chr(ord('A') + 0), row 2 at chr(ord('A') + 2), row 3 at chr(ord('A') + 4), and so on.

2. Print the prefix. Inner loop still walks A..$end on every row.

3. Stay in A–Y. Clamp $rows to 1–13 so $end never walks past Y.

Edge Cases & Pitfalls

Check these before calling the solution done.

$i++

Even lengths

If the outer loop uses $i++ instead of stepping by 2, you get A, AB, ABC, … — not the odd-length pattern.

$i += 2

Coerced to int

$i += 2 on a letter string coerces it to an integer and breaks the outer loop. Use $i = chr(ord($i) + 2).

Inner starts at $i

Missing prefix

Always start the inner loop at 'A'. Starting at $i prints a single letter per row.

PHP_EOL inside

Column of letters

If echo PHP_EOL sits inside the inner loop, each letter lands on its own line. Call it only after the inner loop.

Even end letter

Messy last row

Prefer odd-step endings (A, C, E, …, Y). An even letter like D still runs but breaks the clean odd-width story.

Past Y

Clamp to 13 rows

Row 13 ends at Y. Larger values walk past Z — clamp or stop early.

Time and Space Complexity

ProgramTimeExtra space
Char step / row-count formsO($rows²)O(1)

Total letters printed = 1 + 3 + … + (2r - 1) = r², which is quadratic in the number of rows.

Key Takeaways

  • Odd ends: outer letter steps A, C, E, … with chr(ord($i) + 2).
  • Fresh prefix: every row prints A..end from scratch.
  • Break the row: echo for letters; echo PHP_EOL after the inner loop.
  • Complexity: O(r²) time because total letters equal r²; O(1) extra space.

One line: for each end letter stepping by two, echo A..end, then echo PHP_EOL.

Frequently Asked Questions

It makes the ending letter jump by two (A, C, E, ...) so each row length increases by two characters and stays odd. In PHP write $i = chr(ord($i) + 2).
The outer loop advances the ending letter by 2 (A, C, E, G, I). The inner loop prints every letter from A through that ending letter, so the count is always odd.
Because each row is a fresh prefix A..end. Starting at the end letter would skip earlier letters and change the pattern.
echo sends output without adding a line break. echo PHP_EOL ends the current line with a portable newline. Letters use echo; the row break uses echo PHP_EOL after the inner loop.
1+3+…+(2r-1)=r². For 5 rows that is 25 letters.
O(r²) for r rows, because total echo calls equal r².
The loop still runs, but you no longer get a clean set of odd-length rows aligned to A, C, E, …. Prefer an odd-step ending letter (A, C, E, …, Y) for this pattern.
Prefer reading a row count with trim(fgets(STDIN)) + is_numeric(), or validate a single letter from the input string, and keep the ending letter within A–Z.

Did you know?

Odd numbers add up to perfect squares: 1+3+5+…+(2r-1)=r². That is why this pattern prints exactly r² letters for r rows — the same count that makes the complexity O(r²).

Next: Symmetric Star Center

Alphabet wings with stars filling the middle of each row.

Program 15 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful