Inverted Right-Angled Alphabet Triangle in Java

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

What You’ll Learn

Print an inverted alphabet triangle: the first row is the longest (ABCDE for 5 rows), then each next row drops one letter while still starting from A.

This is the partner to program 1 (growing rows). The inner loop stays the same idea; only the outer loop direction changes.

⭐ Pattern Output

When you run the program with rows = 5:

Output
ABCDE
ABCD
ABC
AB
A
1

Complete Java Program

Fixed rows = 5 version. The outer loop counts down to reduce width; the inner loop prints from A upward.

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

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

🧠 How It Works

1

Outer loop shrinks the row

i goes from rows down to 1, so row lengths are rows, rows-1, ... , 1.

Width shrinks
2

Reset to A each row

Set ch = 'A' at the start of every row so each line begins from A.

Fresh prefix
3

Print i letters with ch++

for (int j = 1; j <= i; j++) runs i times. System.out.print(ch++) prints the current letter then advances to the next code unit, so each row is a fresh forward run from 'A'.

Inner
4

End row, shorter next

System.out.println() ends the line; the outer loop decreases i, so the next row prints one fewer character — the inverted triangle.

println
=

Inverted triangle

n(n + 1)/2 characters total — O(n²) time, O(1) extra space. The outer index shrinks from rows to 1 while each row still starts fresh at 'A'.

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

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Switch the outer loop to count up to get the growing triangle again (Program 1)
  • Start from 'a' for lowercase output
  • Right-align the output with leading spaces
  • Try printing numbers instead of letters

Avoid

  • Forgetting to reset ch each row if you want every line to start from A
  • Allowing rows to exceed 26 without handling wrap-around
  • Mixing up the loop direction when you want an inverted shape

Key Takeaways

1

Counting i down from rows to 1 creates the inverted shape.

2

Reset to 'A' each row to keep each line starting from A.

3

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

4

This is the inverted counterpart of the growing alphabet triangle.

❓ Frequently Asked Questions

Program 1 grows row length from 1 to rows. This program starts with rows letters and decreases to 1.
Resetting ensures each row starts at A and prints the prefix (A, AB, ABC...). Without it, letters would continue across rows.
O(n²) for n rows.

Next: Java Alphabet Pattern 6

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

Program 6 →
Did you know?

You can turn the growing triangle into an inverted triangle by only reversing the outer loop direction—the inner loop stays almost identical.

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