Alphabet Pattern (A to ABCDE) in Java

What You'll Learn
This Java program prints a simple alphabet pattern where each row starts at A and grows by one letter.
Row index i (0-based) prints letters from A through (char)('A' + i), so for n rows the total printed letters are 1 + 2 + … + n = n(n + 1)/2.
⭐ Pattern Output
When you run the program with rows = 5:
A
AB
ABC
ABCD
ABCDEComplete Java Program
Fixed rows = 5 version:
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
for (char ch = 'A'; ch <= (char) ('A' + i); ch++) {
System.out.print(ch);
}
System.out.println();
}
}
}🧠 How It Works
Outer loop: one row per i
for (int i = 0; i < rows; i++) runs once per row. Row i will print i + 1 letters (wider rows on larger i).
Inner loop: inclusive char range
for (char ch = 'A'; ch <= (char) ('A' + i); ch++) walks the alphabet in order; System.out.print(ch) appends each letter on the same line. The cast keeps int arithmetic from widening the loop type oddly.
Finish the row
After the inner loop, System.out.println() emits a newline (platform line separator) so the next row starts flush left. Without it, every row would concatenate on one line.
Growing alphabet rows
Row lengths 1, 2, …, n sum to n(n + 1)/2 prints — O(n²) time, O(1) extra space. Wide lines stay readable on phones because the result glyph scrolls horizontally in the tutorial layout.
Variation — User Input Version
Accept rows with Scanner:
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));
for (int i = 0; i < rows; i++) {
for (char ch = 'A'; ch <= (char) ('A' + i); ch++) {
System.out.print(ch);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Start from a different letter (e.g.,
'C') - Print the pattern in reverse (ABCDE to A)
- Add spaces between letters for readability
- Try a centered alphabet pyramid next
Avoid
- Using magic numbers like
65when'A'is clearer - Allowing
rows > 26without handling wrap-around - Forgetting a newline after each row
- Closing
System.inif you need input later in the app
Key Takeaways
Use nested loops: outer for rows, inner for letters.
Row i prints letters from A to A + i.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
Prefer 'A' over ASCII numbers for readability.
Clamp user input to 26 rows to stay within A to Z.
❓ Frequently Asked Questions
char as numeric Unicode values. So 'A' + 1 becomes 'B', which makes patterns like this easy to generate.'A' with 'a' in the inner loop.n rows, because the program prints 1+2+…+n characters in total.Next: Java Alphabet Pattern 2
Continue to Program 2 for the next alphabet pattern in Java.
In Java, char arithmetic works smoothly for English letters, but for larger alphabets you’ll usually build patterns with strings instead.
10 people found this page helpful
