Sequential Alphabet Triangle in Java

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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

Output
A
B C
D E F
G H I J
K L M N O
1

Complete Java Program (Five Rows, ASCII Loops)

Loop variables i and j use ASCII 6569 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.

Java
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

1

k = 65 ('A')

Global position in the alphabet; it never resets between rows.

Counter
2

Outer i

Each value of i sets how many inner iterations run (1, 2, 3, 4, 5).

Row width
3

System.out.print((char) (k++) + " ")

Print the current letter, append a space, then advance k for the next cell.

Sequence
4

System.out.println()

After the inner loop finishes one row, start the next line.

New row
=

Fifteen letters for five rows

O(n²) letters for n rows.

2

Variation — User Input + Character Literals

Same idea with readable 'A' bounds; last row letter is (char)('A' + rows - 1). Clamp rows to 1–26:

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));
        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 k and for (char i = 'A'; i <= 'E'; i++) — same output, clearer than 65
  • Omit the " " in print if you want a tight triangle
  • Cap rows so k does not pass 'Z'

Avoid

  • Resetting k to 'A' at every row (you would repeat A, BB, … instead of a run-through)
  • Incrementing k only once per row (not enough letters on wider rows)

Key Takeaways

1

Inner loop count matches row width; k carries the alphabet forward.

2

k++ in the output runs once per printed character.

3

O(n²) output size for n rows.

4

ASCII and character-literal loops are interchangeable here.

❓ Frequently Asked Questions

It only repeats the body the right number of times for that row. The value printed is always the current k.
print uses the value of k first, then k advances for the next character.
Set last = (char)('A' + rows - 1), outer for (i = 'A'; i <= last; i++), inner j from 'A' to i, same k++ body.
O(n²) for n rows.

More Java alphabet patterns

One global k is the trick: row loops only decide how many times to print, not which letter.

All Alphabet Patterns →
Did you know?

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.

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.

12 people found this page helpful