Palindromic Alphabet Pyramid

This program prints a palindromic alphabet pyramid.

Problem Statement

Write a Java program that prints a palindromic alphabet pyramid with 5 rows.

Source Code

1 public class PalindromicAlphabetPyramid {
2 public static void main(String[] args) {
3 int n = 5;
4 for(int i=1; i<=n; i++) {
5 for(int j=i; j<n; j++) System.out.print(" ");
6 for(char ch='A'; ch<'A'+i; ch++) System.out.print(ch);
7 for(char ch=(char)('A'+i-2); ch>='A'; ch--) System.out.print(ch);
8 System.out.println();
9 }
10 }
11 }

Program Output

    A    
   ABA   
  ABCBA  
 ABCDCBA 
ABCDEDCBA

Explanation

Each row prints ascending alphabets from 'A' up to the row index, then descending alphabets back to 'A', creating a palindromic sequence of letters per row that forms a symmetric alphabet pyramid.

Quick Links to Explore