Reverse Triangle, E Always First in Java

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:
EDCBA
EDCB
EDC
ED
EComplete Java Program
Fixed rows = 5 version. Outer loop raises the stopping letter; inner loop always starts at E and prints down to that stop.
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
Outer loop raises the floor
The outer variable stop goes from 'A' up to 'E'. It is the lowest letter printed on that row.
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.
Rows get shorter
As stop moves A → B → …, the condition ch >= stop trims one letter from the right each time, giving widths 5, 4, 3, 2, 1.
Line break
System.out.println() after each inner loop separates rows. The variation sets end = (char)('A' + rows - 1) instead of hard-coded 'E'.
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.
Variation — User Input Version
Compute end = (char)('A' + rows - 1) and then print end down to stop for each row:
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
stopis the first printed character (the first printed character is alwaysend) - Printing all the way to A every row if you want shrinking rows (that would repeat full width)
- Letting
rowsexceed 26 without handling wrap-around
Key Takeaways
Every row begins with the same top letter because the inner loop always starts at end.
The outer loop controls where the row stops, so the tail gets shorter each time.
Total printed characters are n(n+1)/2, so runtime is O(n²).
It’s a good example of mixing increasing and decreasing loop directions.
❓ Frequently Asked Questions
end), so the first printed character on every line is that letter.n rows.Next: Java Alphabet Pattern 9
Continue to Program 9 for the next alphabet pattern in Java.
If the inner loop always ran down to 'A', every line would be EDCBA. Changing the stop condition is what creates the shrinking effect.
10 people found this page helpful
