Right-Aligned Reverse Pyramid in Java

What You’ll Learn
Each row is a reverse suffix A, BA, CBA, … padded on the left so letters line up on the right in a fixed-width column.
Same j > i idea as the left half of program 19, without the second mirror loop.
⭐ Pattern Output
Monospace (leading spaces matter):
A
BA
CBA
DCBA
EDCBAComplete Java Program (A–E)
Outer i is the row peak; inner j sweeps the full alphabet slice once per row from E down to A.
public class Main {
public static void main(String[] args) {
int i, j;
for (i = 65; i <= 69; i++) {
for (j = 69; j >= 65; j--) {
if (j > i) {
System.out.print(" ");
} else {
System.out.print((char) j);
}
}
System.out.println();
}
}
}🧠 How It Works
Outer i
How far the reverse run extends this row: down from i to A.
Inner j scan
j always runs from endChar down to startChar, so each line has a fixed width.
if (j > i)
Print a space for letters that belong to higher rows only.
Else branch
Print j, creating a contiguous reverse suffix like CBA.
Leading spaces
Row peak i has endChar - i spaces before the first printed letter.
Variation — User Input
endChar replaces 'E' in the inner loop bound. 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));
char startChar = 'A';
char endChar = (char) ('A' + rows - 1);
for (char i = startChar; i <= endChar; i++) {
for (char j = endChar; j >= startChar; j--) {
if (j > i) {
System.out.print(" ");
} else {
System.out.print(j);
}
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Separate loop: print
endChar - ispaces, then printjfromidown tostartChar - Flip to a forward triangle by scanning
jupward - Use a monospace font so columns line up
Avoid
j >= ifor the space test — that turns the peak column into a space and drops the widest letter on the row- Mixing different
endCharin outer vs inner loops
Key Takeaways
Descending j over a fixed range yields reverse order plus padding.
j > i prints spaces; j <= i prints letters.
Leading space count is endChar - i.
O(n²) for n rows with width n.
❓ Frequently Asked Questions
i to A, only shifted right.endChar through A; shorter rows pad on the left inside that same width.j == i and skip printing the widest letter. Stick to j > i.More Java alphabet patterns
Right alignment is “print the padding, then the payload” inside a fixed column count.
This is the same geometry as printing a left-justified reverse triangle, then shifting every line right by endChar - i spaces.
12 people found this page helpful
