Increasing Start Letter Alphabet Triangle in Java

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

What You’ll Learn

Each row starts one letter later (A, B, C, ...) but always ends at the same last letter (E in the 5-row example). So the widths are 5, 4, 3, 2, 1.

Compare with program 5, where every row restarts at A and shrinks.

⭐ Pattern Output

When you run the program with rows = 5:

Output
ABCDE
BCDE
CDE
DE
E
1

Complete Java Program

Fixed rows = 5 version. The outer loop selects the starting character and the inner loop prints forward up to 'E'.

Java
public class Main {
    public static void main(String[] args) {
        int rows = 5;
        char endChar = 'E';

        for (char start = 'A'; start <= endChar; start++) {
            for (char ch = start; ch <= endChar; ch++) {
                System.out.print(ch);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Pick the row start letter

Outer loop sets start to A, then B, then C, and so on.

Outer
2

Print forward to a fixed end

Inner loop prints from start through endChar (E here), so every row ends at the same letter.

Inner
3

Why the width shrinks

Each time start increases, ch <= endChar spans fewer code points, so the row shortens: five letters for A..E, then four for B..E, and so on.

Geometry
4

Between rows

System.out.println() runs after each inner loop so ABCDE, BCDE, … appear on separate lines. The variation replaces literal 'E' with endChar = (char)('A' + rows - 1).

println
=

Total work

n(n + 1)/2 characters — O(n²) time, O(1) extra space. Using a char outer loop is idiomatic here and matches the variation.

2

Variation — User Input Version

Compute endChar = (char)('A' + rows - 1) for any rows (clamped to 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 endChar = (char) ('A' + rows - 1);

        for (char start = 'A'; start <= endChar; start++) {
            for (char ch = start; ch <= endChar; ch++) {
                System.out.print(ch);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Use lowercase letters by switching to 'a' as the base
  • Compare with program 3 (reverse start, forward run) for a related fixed-edge pattern
  • Right-align lines with leading spaces for a different look
  • Print numbers instead of letters to practice similar logic

Avoid

  • Letting rows exceed 26 without handling wrap-around
  • Hard-coding end letters when you want a dynamic solution (prefer endChar)
  • Forgetting a newline after each row

Key Takeaways

1

Outer loop controls the starting letter (A, B, C, ...).

2

Inner loop prints from the start letter up to the same fixed end letter.

3

Row lengths shrink by one each line: n, n-1, ..., 1.

4

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

❓ Frequently Asked Questions

Because the inner loop always prints up to endChar. Only the start moves forward.
Program 5 always starts at A and shrinks by reducing the end. Program 6 keeps the end fixed and moves the start forward.
O(n²) for n rows.

Next: Java Alphabet Pattern 7

Continue to Program 7 for the next alphabet pattern in Java.

Program 7 →
Did you know?

The right edge stays fixed because the inner loop always ends at endChar—only the start changes each row.

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