Reverse Order Alphabet Triangle in Java

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:
A
BA
CBA
DCBA
EDCBAComplete Java Program
Fixed rows = 5 version. For row i, start at 'A' + i and print backward until 'A'.
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
Outer loop grows the triangle
For i = 0 .. rows-1, row i prints i + 1 letters.
Pick the row’s starting letter
Compute start = 'A' + i so the leftmost letter is A, then B, then C, ...
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.
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.
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.
Variation — User Input Version
Accept rows with Scanner and clamp to 26 (A to Z):
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
rowsexceed 26 without handling wrap-around - Using ASCII numbers (65, 69) when characters (
'A','E') are clearer - Forgetting a newline after each row
Key Takeaways
Row i starts at 'A' + i and prints backward to 'A'.
Each row prints i + 1 letters, forming a triangle.
Time complexity is O(n²) because total output is 1+2+…+n.
This pattern is a good bridge to mirrored and pyramid-style patterns.
❓ Frequently Asked Questions
'A' each time, so the last printed character is always A.'A' with your base letter and adjust the maximum rows accordingly.n rows.Next: Java Alphabet Pattern 5
Continue to Program 5 for the next alphabet pattern in Java.
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.
10 people found this page helpful
