Triangle with Reverse Starting Letter in Java

What You’ll Learn
This pattern mixes ideas from program 1 and program 2: each row grows longer, the first letter of each row moves backward, but letters along the row still move forward.
For 5 rows, the output is E, DE, CDE, BCDE, ABCDE.
⭐ Pattern Output
When you run the program with rows = 5:
E
DE
CDE
BCDE
ABCDEComplete Java Program
Fixed rows = 5 version. The outer loop picks the starting letter for the row, and the inner loop prints forward up to 'E'.
public class Main {
public static void main(String[] args) {
int rows = 5;
char top = 'E';
for (int i = 0; i < rows; i++) {
char start = (char) (top - i);
for (char ch = start; ch <= top; ch++) {
System.out.print(ch);
}
System.out.println();
}
}
}🧠 How It Works
Set the right-edge letter
char top = 'E' fixes the last column for five rows (top = (char)('A' + rows - 1) in the input version). Every row ends at this letter.
Pick the row’s start letter
For row index i, start at top - i: E, D, C, B, A.
Print forward to the top letter
for (char ch = start; ch <= top; ch++) walks forward along the row. Row 0 prints only E; row 1 prints D then E; and so on until the last row spans A..E.
Newline and next i
System.out.println() ends the row. The outer i increments, start = (char)(top - i) moves the left edge one step left, and the inner range widens by one letter.
Reverse start, forward run
Lengths 1 through n sum to n(n + 1)/2 — O(n²) time, O(1) extra space. Generalize with top = (char)('A' + rows - 1) as in the Scanner variation.
Variation — User Input Version
Keep the right edge dynamic by choosing top = (char)('A' + rows - 1). That keeps the last row ending at the top letter and avoids hard-coding 'E':
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 = 0; i < rows; i++) {
char start = (char) (top - i);
for (char ch = start; ch <= top; ch++) {
System.out.print(ch);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Use lowercase letters by switching to
'a'as the base - Right-align the triangle by adding leading spaces
- Compare with program 2 to see how reversing the inner direction changes the row text
- Try printing the same shape with numbers
Avoid
- Letting
rowsexceed 26 without handling wrap-around - Mixing 0-based and 1-based logic when computing
top - i - Hard-coding ASCII codes (prefer
'A','E')
Key Takeaways
The start of each row is top - i, which moves backward each row.
The inner loop prints forward from start to top.
Every row ends at the same letter (the top character) in the fixed-edge version.
Runtime is O(n²) due to 1+2+…+n printing.
❓ Frequently Asked Questions
top. In the classic 5-row example, top is 'E', so each row ends with E.rows to 26 and compute top = 'A' + rows - 1. This stays within uppercase English letters.Next: Java Alphabet Pattern 4
Continue to Program 4 for the next alphabet pattern in Java.
The last character on each row is always top, because the inner loop prints consecutive letters until it reaches top.
10 people found this page helpful
