The mirrored alphabet pyramid puts A in the middle of every row and builds outwards: A, then B A B, then C B A B C. It is the alphabet palindrome pyramid turned inside out.
Quick answer: Row i prints n − i double spaces, then the letters from i down to A ('A' + i − j), then from B back up to i ('A' + j), each letter followed by a space.
This program is part of the String Patterns collection.
Table of contents8 sections
Mirrored Alphabet Pyramid output
Output (n = 4):
A
B A B
C B A B C
D C B A B C D
How the mirrored alphabet pyramid works
In the palindrome pyramid the highest letter sits in the middle. Here it is the other way round: A is in the centre and the letters rise towards both ends. The left half counts down to A and the right half counts back up without repeating A. Each letter takes two characters with its space, so the indent is two spaces per missing letter.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n − ispaces. - Print
iletters (letter numberi − j + 1). - Print
i − 1letters (letter numberj + 1). - Move to the next line.
Key takeaways
- A sits in the centre of every row.
- The left half counts down to A; the right half counts back up from B.
- Each letter with its space is two characters, so the indent uses double spaces.
- The edges read A, B, C, D from top to bottom.
Mirrored Alphabet Pyramid in C
#include <stdio.h>
int main(void) {
int n = 4;
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' + i - j + 1 - 1);
for (int j = 1; j <= i - 1; j++)
printf("%c ", 'A' + j + 1 - 1);
printf("\n");
}
return 0;
}Mirrored Alphabet Pyramid in C++
#include <iostream>
using namespace std;
int main() {
int n = 4;
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' + i - j + 1 - 1) << " ";
for (int j = 1; j <= i - 1; j++)
cout << char('A' + j + 1 - 1) << " ";
cout << endl;
}
return 0;
}Mirrored Alphabet Pyramid in Java
public class MirroredAlphabetPyramid {
public static void main(String[] args) {
int n = 4;
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' + i - j + 1 - 1) + " ");
for (int j = 1; j <= i - 1; j++)
System.out.print((char) ('A' + j + 1 - 1) + " ");
System.out.println();
}
}
}Mirrored Alphabet Pyramid in Python
n = 4
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') + i - j + 1 - 1), end=" ")
for j in range(1, i):
print(chr(ord('A') + j + 1 - 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
How is it different from A, ABA, ABCBA?
In the alphabet palindrome pyramid the highest letter is in the middle. Here A is in the middle and the highest letters are at the edges.
Can I print it with numbers?
Yes: print i − j + 1 and j + 1 instead of letters. Row 3 becomes 3 2 1 2 3.
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.






