Triangle with Reverse Starting Letter in Java

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

What You’ll Learn

This pattern mixes ideas from program 1 and program 2: each row grows longer, the first letter of each row moves backward, but letters along the row still move forward.

For 5 rows, the output is E, DE, CDE, BCDE, ABCDE.

⭐ Pattern Output

When you run the program with rows = 5:

Output
E
DE
CDE
BCDE
ABCDE
1

Complete Java Program

Fixed rows = 5 version. The outer loop picks the starting letter for the row, and the inner loop prints forward up to 'E'.

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

        for (int i = 0; i < rows; i++) {
            char start = (char) (top - i);
            for (char ch = start; ch <= top; ch++) {
                System.out.print(ch);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Set the right-edge letter

char top = 'E' fixes the last column for five rows (top = (char)('A' + rows - 1) in the input version). Every row ends at this letter.

Setup
2

Pick the row’s start letter

For row index i, start at top - i: E, D, C, B, A.

Left edge
3

Print forward to the top letter

for (char ch = start; ch <= top; ch++) walks forward along the row. Row 0 prints only E; row 1 prints D then E; and so on until the last row spans A..E.

Inner
4

Newline and next i

System.out.println() ends the row. The outer i increments, start = (char)(top - i) moves the left edge one step left, and the inner range widens by one letter.

println
=

Reverse start, forward run

Lengths 1 through n sum to n(n + 1)/2O(n²) time, O(1) extra space. Generalize with top = (char)('A' + rows - 1) as in the Scanner variation.

2

Variation — User Input Version

Keep the right edge dynamic by choosing top = (char)('A' + rows - 1). That keeps the last row ending at the top letter and avoids hard-coding 'E':

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 top = (char) ('A' + rows - 1);

        for (int i = 0; i < rows; i++) {
            char start = (char) (top - i);
            for (char ch = start; ch <= top; 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
  • Right-align the triangle by adding leading spaces
  • Compare with program 2 to see how reversing the inner direction changes the row text
  • Try printing the same shape with numbers

Avoid

  • Letting rows exceed 26 without handling wrap-around
  • Mixing 0-based and 1-based logic when computing top - i
  • Hard-coding ASCII codes (prefer 'A', 'E')

Key Takeaways

1

The start of each row is top - i, which moves backward each row.

2

The inner loop prints forward from start to top.

3

Every row ends at the same letter (the top character) in the fixed-edge version.

4

Runtime is O(n²) due to 1+2+…+n printing.

❓ Frequently Asked Questions

Because the inner loop always prints up to top. In the classic 5-row example, top is 'E', so each row ends with E.
Clamp rows to 26 and compute top = 'A' + rows - 1. This stays within uppercase English letters.
O(n²) for n rows, since total printed characters are \(n(n+1)/2\).

Next: Java Alphabet Pattern 4

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

Program 4 →
Did you know?

The last character on each row is always top, because the inner loop prints consecutive letters until it reaches top.

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