Reverse Right-Angled Triangle Alphabet Pattern in Java

What You’ll Learn
How to print a reverse alphabet right-angled triangle: each row has one more character than the previous row, and letters run backward along the row (E, ED, EDC, ... for 5 rows).
This is the reverse counterpart of program 1, which prints A, AB, ABC, ...
⭐ Pattern Output
When you run the program with rows = 5:
E
ED
EDC
EDCB
EDCBAComplete Java Program
Fixed rows = 5 version. Reset ch to 'E' at the beginning of each row, then print i letters with ch--.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
char ch = 'E';
for (int j = 1; j <= i; j++) {
System.out.print(ch--);
}
System.out.println();
}
}
}🧠 How It Works
Outer loop for rows
Loop i = 1 .. rows. Row i prints exactly i characters.
Reset the starting letter
Set ch = 'E' at the start of each row, so every row begins from the same top letter.
Inner loop prints and decrements
for (int j = 1; j <= i; j++) runs i times. System.out.print(ch--) uses postfix decrement: it prints the current char, then steps backward toward 'A'.
Next row
System.out.println() ends the line; the outer loop’s next iteration sets ch = 'E' again so each row starts from the same top letter (for the fixed five-row demo).
Reverse alphabet triangle
Total printed characters are 1 + 2 + … + n = n(n + 1)/2 — O(n²) time, O(1) extra space. The variation replaces hard-coded 'E' with top = (char)('A' + rows - 1).
Variation — User Input Version
For rows rows, compute the top letter as 'A' + rows - 1, then use it as the starting ch for every 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 top = (char) ('A' + rows - 1);
for (int i = 1; i <= rows; i++) {
char ch = top;
for (int j = 1; j <= i; j++) {
System.out.print(ch--);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Use lowercase letters by starting from
'e'/'a' - Print the forward alphabet triangle using program 1
- Add spaces between letters for readability
- Try making a centered alphabet pyramid next
Avoid
- Hard-coding
'E'when you want a fully dynamic program (computetop) - Letting
rows > 26without handling wrap-around - Forgetting a newline after each row
- Closing
System.inif you need input later in the same app
Key Takeaways
Row i prints i letters, so the triangle widens one character per row.
Reset the starting letter at the beginning of each row to keep the pattern shape.
Generalize with top = (char)('A' + rows - 1) and print using ch--.
Total printed characters are \(n(n+1)/2\), so runtime is O(n²).
❓ Frequently Asked Questions
ch ensures every row begins with the same top letter, so the pattern stays E, ED, EDC, ... instead of continuing the decrement across rows.'e' (fixed) or 'a' (dynamic) as the base character instead of 'E' / 'A'.n rows, because the program prints 1+2+…+n characters overall.Next: Java Alphabet Pattern 3
Continue to Program 3 for the next alphabet pattern in Java.
System.out.print(ch--) uses the post-decrement value for printing, so you see the letter first, then ch moves to the previous character.
10 people found this page helpful
