Shape Rule
Odd widths 1, 3, 5…
Each row prints the prefix A..end where end is A, C, E, G, I.

Each row is a block of letters from A through the next “odd step” in the alphabet: A, ABC, ABCDE, ABCDEFG, ABCDEFGHI. The outer loop uses i += 2 (A, C, E, G, I). Compare Program 1 (step 1) and Program 13 (running counter). Includes a live preview, worked Java examples, edge cases, and complexity.
Odd widths 1, 3, 5…
Each row prints the prefix A..end where end is A, C, E, G, I.
Step by 2
for (char i = 'A'; i <= 'I'; i += 2) picks the end letter.
Print A..i
for (char j = 'A'; j <= i; j++) restarts at A every row.
Same line / next line
Letters use print; end each row with println().
1–13 rows
Pick a row count and draw the odd-length triangle in the browser.
Complexity
Total letters = r²; extra memory stays O(1).
An odd-length alphabet triangle grows by two letters on each new line. Every row still starts at A, but the ending letter jumps A → C → E → G → I, so widths are 1, 3, 5, 7, 9.
In Java you usually solve it with two nested for loops: the outer loop steps the end letter by 2, the inner loop prints A through that end letter, then System.out.println() moves to the next line.
It shows that changing only the outer step (1 vs 2) transforms Program 1 into an odd-width triangle — and that odd-number sums equal perfect squares, which makes complexity analysis concrete.
Row lengths are 1, 3, 5, 7, 9, …
Outer end letter jumps with i += 2.
Inner loop always restarts at A.
Odd sum identity: total prints equal r².
In short: for each end letter i stepping A, C, E, …, print A..i with System.out.print, then call System.out.println().
Given a row count r (or a fixed odd-step ending letter like 'I'), print a left-aligned triangle of alphabet prefixes with odd lengths.
// First 5 rows (conceptual shape)
// A
// ABC
// ABCDE
// ABCDEFG
// ABCDEFGHI | Item | Type | Description |
|---|---|---|
rows / end letter | int / char | Number of odd-length lines (1–13 for A–Y), or last end letter such as 'I'. |
| Printed output | text | Left-aligned rows; row k prints letters from A through 'A' + 2*(k-1). |
for end in A, C, E, ... up to last:
for ch from A to end:
print ch (no newline)
print newline | Approach | Idea | Best for |
|---|---|---|
Char i += 2 | Outer end letter steps by two | Learning and interviews |
| Row index formula | end = 'A' + 2*(row-1) | Clearer when input is a row count |
| Goal | Pattern |
|---|---|
| Step end letters | for (char i = 'A'; i <= 'I'; i += 2) |
| Print prefix A..i | for (char j = 'A'; j <= i; j++) System.out.print(j); |
| End the row | System.out.println(); |
| End from row index | end = (char)('A' + 2 * (row - 1)); |
| Step-1 triangle | See Program 1 (A, AB, ABC, …) |
Same triangle — different roles for each tool.
same linePrints a letter without moving to the next line
new lineEnds the current row after the prefix is printed
odd endsJumps the ending letter A → C → E …
i += 2In Java, use compound assignment: i += 2
Reach for this triangle when practicing loop steps and odd-width prefixes.
Change only the outer step from 1 to 2 for odd widths.
Practice += 2 on chars and int row formulas.
Odd sums equal squares — count printed letters for small r.
Next: symmetric alphabet rows with a star center.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that links loop step size, odd widths, and the classic odd-sum = square identity.
Choose a row count between 1 and 13 and draw the odd-length alphabet triangle in the browser.
Three complete Java programs — fixed through I, ending-letter input, and a row-count formula. Click View Output to reveal sample console results.
Print five odd-length rows with i += 2.
'I'Hard-coded ending letter — ideal for first demos and screenshots.
public class OddLengthTriangle {
public static void main(String[] args) {
for (char i = 'A'; i <= 'I'; i += 2) {
for (char j = 'A'; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
} When i = 'A', the inner loop prints A. When i = 'C', it prints ABC, and so on through ABCDEFGHI. In Java, compound assignment i += 2 works on char (plain i = i + 2 needs a cast).
Let the user choose the last ending letter.
Read an odd-step ending letter (A, C, E, …). Prefer validating a single A–Z character in real apps.
import java.util.Scanner;
public class OddLengthInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the ending letter (odd step like I): ");
char end = sc.next().toUpperCase().charAt(0);
for (char i = 'A'; i <= end; i += 2) {
for (char j = 'A'; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
sc.close();
}
} Same nested-loop core as Example 1; only the outer upper bound changes. Prefer odd-step endings (A, C, E, …) so every row length stays odd from the start.
Drive the pattern from a row count instead of an ending letter.
end = 'A' + 2*(row-1)Clear when the user enters how many rows to print.
public class OddLengthRows {
public static void main(String[] args) {
int rows = 5;
for (int row = 1; row <= rows; row++) {
char end = (char)('A' + 2 * (row - 1));
for (char j = 'A'; j <= end; j++) {
System.out.print(j);
}
System.out.println();
}
}
} Row 1 ends at A + 0, row 2 at A + 2, row 3 at A + 4, and so on. Clamp rows to 1–13 so end stays within A–Y.
Import java.util.Scanner when reading input. Choose a last end letter or a row count.
i takes A, C, E, G, I by using i += 2.
j always starts at A and prints every letter up to the current i.
System.out.println() ends the row so the next outer iteration starts fresh.
Total letters: 1+3+…+(2r-1) = r² — O(r²) time, O(1) extra memory.
'I'Trace each outer-loop value of i and count how many letters the inner loop prints.
i | Inner j range | Printed row | Length |
|---|---|---|---|
'A' | 'A'..'A' | A | 1 |
'C' | 'A'..'C' | ABC | 3 |
'E' | 'A'..'E' | ABCDE | 5 |
'G' | 'A'..'G' | ABCDEFG | 7 |
'I' | 'A'..'I' | ABCDEFGHI | 9 |
Total letter prints: 1 + 3 + 5 + 7 + 9 = 25 = 5².
Where this tiny pattern (and its step-by-2 idea) shows up beyond the homework prompt.
Clearest demo that the outer increment controls width growth.
Example: change += 2 to += 1 and watch Program 1 appear.
Teach step size as a one-line difference between patterns.
Example: side-by-side A/AB/ABC vs A/ABC/ABCDE.
Count letters to see that odd totals equal squares.
Example: 5 rows → 25 = 5² prints.
Lowercase or spaced letters once the loops work.
Example: start from 'a' with the same += 2.
Square totals make O(r²) concrete without triangular formulas.
Example: r = 10 → 100 letter prints.
Practice both ending-letter and row-count APIs for the same shape.
Example: map rows=3 ↔ end='E'.
Pro Tip: say “outer picks the odd end letter, inner prints A through that end” before coding — that story prevents forgetting to restart at A.
Why this pattern earns a spot right after the classic A/AB/ABC triangle.
Wrong step size shows up immediately as consecutive widths instead of odd ones.
Only nested loops and a step of 2 — no arrays required.
Flip back to Program 1 by changing the outer step to 1.
Total work is exactly r² — memorable for interviews.
Pro Tip: learn the i += 2 version first; treat the row-index formula as an equivalent rewrite afterward.
Small habits that keep odd-length alphabet code clean.
Write i += 2 — compound assignment works; avoid i = i + 2 without a cast.
Use A, C, E, …, Y when you want clean odd lengths from row 1.
Inner loop must begin at 'A' every row for this prefix shape.
Row 13 ends at Y; row 14 would leave A–Z.
Trace 3 rows (A / ABC / ABCDE) on paper before coding larger demos.
Pro Tip: if you get A, AB, ABC instead of A, ABC, ABCDE, you used step 1 instead of step 2.
Mistakes that commonly break odd-length alphabet patterns.
You get Program 1’s consecutive widths (A, AB, ABC, …).
→ Keep i += 2 (or 2*(row-1) for the end letter).
iSkipping A produces single letters or wrong prefixes.
→ Always for (char j = 'A'; j <= i; j++).
(char) Casti = i + 2 does not compile for char without a cast.
→ Use i += 2 or an int-based end formula.
charAt(0)Empty tokens or non-letters produce unexpected ending letters.
→ Validate a single A–Z letter, or take a row count with hasNextInt().
Beyond 13 rows the end letter leaves A–Z.
→ Clamp to 1–13 or stop when end > 'Z'.
Check these inputs before calling the solution done.
Output is just A on one line.
Prints A / ABC / ABCDE.
Still runs, but odd-length alignment from A is messier — prefer odd-step ends.
End letter Y; 13² = 169 prints.
Validate before taking charAt(0).
Same loops work with 'a' and += 2.
Try these variations to lock in the pattern.
r²r² — hence O(r²) time.A.i += 2 (compound assignment).Quick Takeaway: outer loop steps the end letter by 2, inner loop prints A through that end, then break the line — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(r²) | O(1) |
Because 1+3+…+(2r-1)=r², the letter count is exactly a perfect square.
The odd-length alphabet triangle is a small nested-loop exercise with lasting payoff: outer step size, prefix printing, and the odd-sum = square identity. Master the i += 2 version, then optionally drive it from a row count with 'A' + 2*(row-1).
Practice the three examples above, then continue to Program 15’s symmetric alphabet-with-stars pattern.
Step the end letter by 2, always restart the inner loop at A, and remember total prints equal r².
i += 2 (or the row-index end formula)'A' every rowr² when asked about complexityi = i + 2 without a cast (use i += 2)Print the odd-length triangle the beginner-friendly way.
Odd widths via step 2
DefinitionEnd letters A, C, E…
CodePrints A..end each row
CodeEnds each row
I/OO(r²) time
AnalysisOdd 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 up: symmetric alphabet rows with stars filling the center.
12 people found this page helpful