Reverse Order Alphabet Triangle in Java

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

What You’ll Learn

Build a triangle where the first letter of each row increases (A, B, C, ...), but letters inside each row are printed in reverse down to A: A, BA, CBA, DCBA, EDCBA.

Compare with program 1 (A, AB, ABC, ...) and program 3 (reverse start, forward along the row).

⭐ Pattern Output

When you run the program with rows = 5:

Output
A
BA
CBA
DCBA
EDCBA
1

Complete Java Program

Fixed rows = 5 version. For row i, start at 'A' + i and print backward until 'A'.

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

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

🧠 How It Works

1

Outer loop grows the triangle

For i = 0 .. rows-1, row i prints i + 1 letters.

Rows
2

Pick the row’s starting letter

Compute start = 'A' + i so the leftmost letter is A, then B, then C, ...

Left edge
3

Print backward to A

for (char ch = start; ch >= 'A'; ch--) walks from the row’s peak letter down to 'A'. Row 0 is A only; row 1 is B then A; the last row prints the full reverse run to E.

Inner
4

Break the line

System.out.println() after the inner loop starts the next row. Only print is used inside the row so all letters stay on one logical line.

println
=

Reverse-order rows

Again 1 + 2 + … + n letters — O(n²) time, O(1) extra space. Row lengths still grow 1, 2, …, n; only the inner sweep runs backward.

2

Variation — User Input Version

Accept rows with Scanner and clamp to 26 (A to Z):

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++) {
            char start = (char) ('A' + i);
            for (char ch = start; ch >= 'A'; ch--) {
                System.out.print(ch);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Print lowercase letters by starting from 'a' instead of 'A'
  • Right-align the triangle with leading spaces
  • Compare with program 1 to see forward vs reverse rows
  • Use a different base letter (e.g., start from 'C')

Avoid

  • Letting rows exceed 26 without handling wrap-around
  • Using ASCII numbers (65, 69) when characters ('A', 'E') are clearer
  • Forgetting a newline after each row

Key Takeaways

1

Row i starts at 'A' + i and prints backward to 'A'.

2

Each row prints i + 1 letters, forming a triangle.

3

Time complexity is O(n²) because total output is 1+2+…+n.

4

This pattern is a good bridge to mirrored and pyramid-style patterns.

❓ Frequently Asked Questions

Because the inner loop prints down to 'A' each time, so the last printed character is always A.
Yes. Replace 'A' with your base letter and adjust the maximum rows accordingly.
It’s O(n²) for n rows.

Next: Java Alphabet Pattern 5

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

Program 5 →
Did you know?

This pattern keeps the right edge fixed at A while the left edge climbs A, B, C, ...—the mirror idea of patterns where the right edge stays fixed at a higher letter.

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