Reverse Triangle, E Always First in Java

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

What You’ll Learn

Every line begins with E and runs backward in the alphabet, but lines get shorter: EDCBA, EDCB, EDC, ED, E.

Contrast program 7 (starting letter changes each row) with this one (start letter stays fixed at E).

⭐ Pattern Output

When you run the program with rows = 5:

Output
EDCBA
EDCB
EDC
ED
E
1

Complete Java Program

Fixed rows = 5 version. Outer loop raises the stopping letter; inner loop always starts at E and prints down to that stop.

Java
public class Main {
    public static void main(String[] args) {
        char end = 'E';

        for (char stop = 'A'; stop <= end; stop++) {
            for (char ch = end; ch >= stop; ch--) {
                System.out.print(ch);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Outer loop raises the floor

The outer variable stop goes from 'A' up to 'E'. It is the lowest letter printed on that row.

Lower bound
2

Inner loop always starts at E

The inner loop starts from end (E) every time, so the first character on each line is always E.

Fixed first letter
3

Rows get shorter

As stop moves AB → …, the condition ch >= stop trims one letter from the right each time, giving widths 5, 4, 3, 2, 1.

5, 4, 3, 2, 1
4

Line break

System.out.println() after each inner loop separates rows. The variation sets end = (char)('A' + rows - 1) instead of hard-coded 'E'.

println
=

Total work

n(n + 1)/2 characters — O(n²) time, O(1) extra space. First column is always end; the floor stop rises each row.

2

Variation — User Input Version

Compute end = (char)('A' + rows - 1) and then print end down to stop for each row:

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

        for (char stop = 'A'; stop <= end; stop++) {
            for (char ch = end; ch >= stop; ch--) {
                System.out.print(ch);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Try lowercase by switching to 'a' as the base
  • Compare with program 2 (E, ED, EDC...) where the row start changes differently
  • Add spaces and align the triangle to the right
  • Replace letters with numbers to practice the same clipping logic

Avoid

  • Assuming stop is the first printed character (the first printed character is always end)
  • Printing all the way to A every row if you want shrinking rows (that would repeat full width)
  • Letting rows exceed 26 without handling wrap-around

Key Takeaways

1

Every row begins with the same top letter because the inner loop always starts at end.

2

The outer loop controls where the row stops, so the tail gets shorter each time.

3

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

4

It’s a good example of mixing increasing and decreasing loop directions.

❓ Frequently Asked Questions

Because the inner loop always starts at the same top letter (end), so the first printed character on every line is that letter.
Program 7 changes the first letter each row (E, D, C, ...). Program 8 keeps the first letter fixed and only shortens the tail.
O(n²) for n rows.

Next: Java Alphabet Pattern 9

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

Program 9 →
Did you know?

If the inner loop always ran down to 'A', every line would be EDCBA. Changing the stop condition is what creates the shrinking effect.

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