Right-Aligned Reverse Alphabet Pyramid in Java

What You’ll Learn
This is a companion to program 22: it uses the same fixed-width grid (two spaces for padding + %2c for letters), but prints a reverse alphabet slice on each row.
Use a monospace font in your terminal so columns stay even.
⭐ Pattern Output
Output for 5 rows (E down to A):
E
E D
E D C
E D C B
E D C B AJava Program (Fixed A–E)
First print padding pairs, then print letters from E down to the current row letter.
public class Main {
public static void main(String[] args) {
int i, j;
for (i = 69; i >= 65; i--) {
for (j = 65; j < i; j++) {
System.out.print(" ");
}
for (j = 69; j >= i; j--) {
System.out.format("%2c", j);
}
System.out.println();
}
}
}🧠 How It Works
Outer loop i (E → A)
Top row prints 1 letter (E), bottom row prints 5 letters (E D C B A).
Padding loop j = A .. i-1
Each iteration prints " ". As i decreases, padding shrinks (four pairs down to zero).
Letter loop j = E .. i
System.out.format("%2c", j) gives each letter a 2-column cell to match the two-space padding.
Quick check
At i = C (67), you get two padding pairs and then E D C.
Variation — User Input
Compute end as 'A' + rows - 1, then run the same two-loop structure. Clamp rows to 1–26.
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: ");
int rows = sc.nextInt();
rows = Math.max(1, Math.min(rows, 26));
final int start = 'A';
int end = start + rows - 1;
for (int i = end; i >= start; i--) {
for (int j = start; j < i; j++) {
System.out.print(" ");
}
for (int j = end; j >= i; j--) {
System.out.format("%2c", j);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Switch to character loops (
for (char i = (char) end; i >= 'A'; i--)) - Use lowercase by starting at
'a'+ offset - Validate input so
rowsstays in 1–26
Avoid
- Changing padding width without matching the
%2cfield width - Running past
Zwithout accounting for non-letter characters
Key Takeaways
Use a dedicated padding loop to right-align the triangle.
The letter loop prints end down to i each row (a reverse slice).
%2c + " " keeps a consistent grid in the console.
O(n²) time for n rows.
❓ Frequently Asked Questions
More Java alphabet patterns
Splitting padding and content into two loops is a clean way to build aligned console pyramids.
With 5 rows, you still print 1 + 2 + 3 + 4 + 5 = 15 letters in total—same count as program 22, just arranged differently.
12 people found this page helpful
