Sequential Alphabet Triangle in Java

What You’ll Learn
Each row is wider than the last, but letters stay in order across the whole shape: A, then B C, then D E F, up to K L M N O for five rows (with a space after each letter).
This differs from program 1 style triangles where each column is derived from j; here one counter k walks the alphabet continuously.
⭐ Pattern Output
For 5 rows (space after each letter):
A
B C
D E F
G H I J
K L M N OComplete Java Program (Five Rows, ASCII Loops)
Loop variables i and j use ASCII 65–69 to control row width. The actual letters come from k, post-incremented in each print. Use (char)(k++) so the cast applies to the value printed before k advances.
public class Main {
public static void main(String[] args) {
int i, j;
int k = 65;
for (i = 65; i <= 69; i++) {
for (j = 65; j <= i; j++) {
System.out.print((char) (k++) + " ");
}
System.out.println();
}
}
}🧠 How It Works
k = 65 ('A')
Global position in the alphabet; it never resets between rows.
Outer i
Each value of i sets how many inner iterations run (1, 2, 3, 4, 5).
System.out.print((char) (k++) + " ")
Print the current letter, append a space, then advance k for the next cell.
System.out.println()
After the inner loop finishes one row, start the next line.
Fifteen letters for five rows
O(n²) letters for n rows.
Variation — User Input + Character Literals
Same idea with readable 'A' bounds; last row letter is (char)('A' + rows - 1). Clamp rows to 1–26:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows (max 26): ");
int rows = sc.nextInt();
rows = Math.max(1, Math.min(rows, 26));
char k = 'A';
char last = (char) ('A' + rows - 1);
char i, j;
for (i = 'A'; i <= last; i++) {
for (j = 'A'; j <= i; j++) {
System.out.print(k++ + " ");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Fixed five rows with
char kandfor (char i = 'A'; i <= 'E'; i++)— same output, clearer than65 - Omit the
" "inprintif you want a tight triangle - Cap
rowssokdoes not pass'Z'
Avoid
- Resetting
kto'A'at every row (you would repeatA,BB, … instead of a run-through) - Incrementing
konly once per row (not enough letters on wider rows)
Key Takeaways
Inner loop count matches row width; k carries the alphabet forward.
k++ in the output runs once per printed character.
O(n²) output size for n rows.
ASCII and character-literal loops are interchangeable here.
❓ Frequently Asked Questions
k.print uses the value of k first, then k advances for the next character.last = (char)('A' + rows - 1), outer for (i = 'A'; i <= last; i++), inner j from 'A' to i, same k++ body.More Java alphabet patterns
One global k is the trick: row loops only decide how many times to print, not which letter.
In C you might use printf("%c ", k++); in Java, System.out.print((char)(k++) + " ") with an int k (or print(k++ + " ") with char k) advances the alphabet once per cell the same way.
12 people found this page helpful
