Odd-Length Alphabet Triangle in Java

What You’ll Learn
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 (values A, C, E, G, I); compare program 13, where width grows by one each row and a separate counter drives the letters.
⭐ Pattern Output
Five rows, no spaces between letters:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHIComplete Java Program (Classic Codetofun Form)
Outer i steps by 2 from 65 while i <= 74 (same effect as i <= 73 or i <= 'I'). Inner j prints every code from 65 through i.
public class Main {
public static void main(String[] args) {
int i, j;
for (i = 65; i <= 74; i += 2) {
for (j = 65; j <= i; j++) {
System.out.print((char) j);
}
System.out.println();
}
}
}🧠 How It Works
i += 2
Outer loop visits A, C, E, G, I — every other capital letter.
Inner j from 65 to i
Always rebuilds the prefix from the start of the alphabet up to the current end letter.
System.out.print((char) j)
Emits each letter in that range; no separate running counter is needed.
New line per outer step
After finishing one prefix, move to the next wider odd-length row.
25 characters total
1+3+5+7+9 = 25; for r rows this is r² prints.
Variation — Ending Letter from Input
User supplies the last outer value (odd-position letter from A: A, C, E, …). Read a non-empty token and take the first character; validate it is an uppercase letter in a sensible range if you need stricter input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the ending letter (e.g. E for A,C,E rows): ");
String token = sc.next().trim();
char endChar = token.charAt(0);
int i, j;
for (i = 'A'; i <= endChar; i += 2) {
for (j = 'A'; j <= i; j++) {
System.out.print((char) j);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Clearer bound:
for (i = 'A'; i <= 'I'; i += 2)with innerjfrom'A'toi - Even-end variant: start at
'B'and step by 2 for B, D, F, … - Validate
endCharif you need a predictable ladder shape
Avoid
- Mixing up “odd length” with “odd ASCII code” — here it is odd count per row (1, 3, 5, …)
- Calling
token.charAt(0)on an empty string afternext()
Key Takeaways
i += 2 picks every other letter for the row end marker.
Inner loop always runs 'A'..i, so widths grow by 2 each row.
Total prints for r such rows: r² (here 5² = 25).
Prefer i <= 'I' over a loose numeric bound when you want five rows through I.
❓ Frequently Asked Questions
i = 73 ('I'). Using i <= 73 or i <= 'I' states the intent more clearly.i = 'A' (since 'C' would exceed B). For a full ladder through B, you would change the loop design.More Java alphabet patterns
Changing the outer step size reshapes how fast each row grows—compare program 1’s step-by-one width with this odd-width ladder.
Row lengths 1, 3, 5, …, (2r−1) sum to r². So five rows always print exactly 25 characters, same as a 5×5 block of letters would if you filled it cleverly.
12 people found this page helpful
