Alphabet Pattern (A to ABCDE) in Java

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n(n+1)/2 letters total

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:

Output
A
AB
ABC
ABCD
ABCDE
1

Complete Java Program

Fixed rows = 5 version:

Java
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

1

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).

Outer
2

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.

Inner
3

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.

println
=

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.

2

Variation — User Input Version

Accept rows with Scanner:

Java
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 65 when 'A' is clearer
  • Allowing rows > 26 without handling wrap-around
  • Forgetting a newline after each row
  • Closing System.in if you need input later in the app

Key Takeaways

1

Use nested loops: outer for rows, inner for letters.

2

Row i prints letters from A to A + i.

3

Total printed letters are \(n(n+1)/2\), so runtime is O(n²).

4

Prefer 'A' over ASCII numbers for readability.

5

Clamp user input to 26 rows to stay within A to Z.

❓ Frequently Asked Questions

Java stores char as numeric Unicode values. So 'A' + 1 becomes 'B', which makes patterns like this easy to generate.
Yes. Replace 'A' with 'a' in the inner loop.
It’s O(n²) for 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.

Program 2 →
Did you know?

In Java, char arithmetic works smoothly for English letters, but for larger alphabets you’ll usually build patterns with strings instead.

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.

10 people found this page helpful