The alphabet palindrome pyramid prints rows that read the same forwards and backwards: A, ABA, ABCBA. Each row climbs to its middle letter and comes back down.
Quick answer: Each row has three parts: n β i spaces, the letters A up to the i-th letter, then the letters back down to A without repeating the middle one. The falling part prints 'A' + i β j β 1 for j from 1 to i β 1.
This program is part of the Pyramid Patterns collection.
Table of contents8 sections
Alphabet Palindrome Pyramid output
Output (n = 5):
A ABA ABCBA ABCDCBA ABCDEDCBA

Same shape, other characters: the Continuous Number Pyramid, Full Pyramid Star Pattern and Palindrome Number Pyramid programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the alphabet palindrome pyramid works
This is the palindrome number pyramid (1, 121, 12321) with letters. The rising half prints the letters 1 to i; the falling half prints i β 1 down to 1. Leaving the middle letter out of the falling half is what keeps each row an odd length and stops the peak from doubling.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n β ispaces. - Print
iletters (letter numberj). - Print
i β 1letters (letter numberi β j). - Move to the next line.
Key takeaways
- Every row is a palindrome with its peak in the middle.
- The rising half has
iletters, the falling halfi β 1. - The rows have no spaces between letters, so
n β ileading spaces centre them. - Print the falling rows again in reverse to make an alphabet diamond.
Alphabet Palindrome Pyramid in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
printf(" ");
for (int j = 1; j <= i; j++)
printf("%c", 'A' + j - 1);
for (int j = 1; j <= i - 1; j++)
printf("%c", 'A' + i - j - 1);
printf("\n");
}
return 0;
}Alphabet Palindrome Pyramid in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << char('A' + j - 1);
for (int j = 1; j <= i - 1; j++)
cout << char('A' + i - j - 1);
cout << endl;
}
return 0;
}Alphabet Palindrome Pyramid in Java
public class AlphabetPalindromePyramid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print((char) ('A' + j - 1));
for (int j = 1; j <= i - 1; j++)
System.out.print((char) ('A' + i - j - 1));
System.out.println();
}
}
}Alphabet Palindrome Pyramid in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, i + 1):
print(chr(ord('A') + j - 1), end="")
for j in range(1, i):
print(chr(ord('A') + i - j - 1), end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(nΒ²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
Why does the middle letter appear only once?
The falling half starts one letter below the peak. If it started at the peak, every row would have a doubled middle letter, such as ABCCBA.
How do I turn it into a diamond?
After the pyramid, print the rows again from n β 1 down to 1 with the same formulas.
How do I let the user choose the number of rows?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






