Symmetric Alphabet Pyramid in Java

What You’ll Learn
This pattern prints centered palindromic rows: A, ABA, ABCBA, up to ABCDEDCBA.
We pad with spaces, print the left half ascending, then print the right half descending without repeating the middle letter.
⭐ Pattern Output
Output for 5 rows (A..E):
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAJava Program (Reference Logic)
Uses an n = k - 1 bridge variable like the reference to print the mirrored tail.
public class Main {
public static void main(String[] args) {
int i, j, k, m, n;
for (i = 65; i <= 69; i++) {
for (j = 69; j >= i; j--)
System.out.print(" ");
for (k = 65; k <= i; k++)
System.out.print((char) k);
n = k - 1;
for (m = 65; m < i; m++)
System.out.print((char) (--n));
System.out.println();
}
}
}🧠 How It Works
Leading spaces
j runs from 69 down to i; each step prints a space so shorter rows shift right and the pyramid aligns under the widest line.
Ascending half
k from 65 through i prints (char) k, building the left side including the peak letter for that row.
n = k - 1
When the second loop exits, k == i + 1. Setting n = k - 1 puts n back on the peak so the first --n yields i - 1 for the mirror.
Mirror with --n
m runs i - 65 times (zero when i == 65). Each System.out.print((char) (--n)) prints the next letter down without repeating the center character.
Palindrome row
The peak prints only in the ascending loop; the descending loop walks from i - 1 down to A. Five outer rows with proportional inner work → O(n²) for n letters.
Variation — User Input (char literals)
Same pattern using 'A'…endChar; descending half starts at i - 1.
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 >= i; j--)
System.out.print(' ');
for (char k = startChar; k <= i; k++)
System.out.print(k);
for (char k = (char) (i - 1); k >= startChar; k--)
System.out.print(k);
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Print a space between letters for a wider pyramid
- Use lowercase by starting at
'a' - Limit
rowsso output stays within letters A..Z
Avoid
- Printing the peak letter again on the way down (duplicates the center)
- Mixing different padding widths without checking alignment
Key Takeaways
Build rows as palindromes: ascending then descending.
Don’t duplicate the middle letter: start descending from i - 1.
Leading spaces keep the pyramid centered.
O(n²) output for n rows.
❓ Frequently Asked Questions
i - 1 (or uses --n), so the peak is printed only once.More Java alphabet patterns
Once you can build one palindromic row, you can generate diamonds and more complex symmetric patterns.
Row lengths here are odd: 1, 3, 5, 7, 9… because each row is a palindrome with one center character.
12 people found this page helpful
