Java Palindromic Alphabet Pyramid Pattern

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A palindromic alphabet pyramid prints each row as letters climbing from A to a peak, then descending back to A — so every full row reads the same forward and backward.

Remember
Rule: print A..peak, then (peak-1)..A (center once)

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA     ← 5 rows

Unlike Program 1 (only the left half), the mirror starts at peak - 1 so the middle letter is not doubled. Row length is always odd: 2r - 1.

How to Solve It

For each row, pick a peak letter, print up to it, then mirror back without reprinting the peak.

MethodIdeaBest for
Up then downTwo loops: A..peak then (peak-1)..ALearning, interviews, left-aligned demos
CenteredSame letters plus leading spacesPyramid layout under the widest row

Pseudocode

Pseudocode
for r from 0 to n - 1:
    peak = 'A' + r
    for ch from 'A' to peak:
        print ch
    for ch from peak - 1 down to 'A':
        print ch
    print newline

Cheat sheet

GoalPattern
Pick the peakchar peak = (char)('A' + r);
Ascending halffor (char ch = 'A'; ch <= peak; ch++) System.out.print(ch);
Descending halffor (char ch = (char)(peak - 1); ch >= 'A'; ch--) System.out.print(ch);
Array formForward 0..i, then reverse i-1..0 into alpha[]
Center the rowfor (int s = 0; s < n - r - 1; s++) System.out.print(" ");
End the rowSystem.out.println();
A–Z-safe heightClamp n to 1–26

Printing Letters vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach letter (and optional pad spaces)
System.out.printlnEnds the current lineAfter both up and down halves

Print letters without a newline, then end the row once.

Live Preview

Change the row count and the palindrome pyramid updates instantly — including peak letter and letter totals.

Whole numbers from 1 to 10. Row r (0-based) peaks at A + r; total letters equal n².

Live result 5 rows · peak E · 25 letters
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

Worked Walkthrough — n = 4

Trace each peak, the ascending half, the descending half, and the full palindrome.

Row rPeakUpDownPrinted row
0AA(none)A
1BABAABA
2CABCBAABCBA
3DABCDCBAABCDCBA

Lengths: 1 + 3 + 5 + 7 = 16 = 4². Starting the down loop at the peak would wrongly print ABCCBA on row 2.

Java Programs

Three complete programs: fixed A–E with a char array, Scanner input, and a centered layout. Use View Output to reveal sample results.

Example 1 — Fixed A–E (Char Array)

Print the forward part A..i, then print back from i-1..A.

Java
public class PalindromePyramid {
    public static void main(String[] args) {
        char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

        for (int i = 0; i <= 4; i++) {
            for (int j = 0; j <= i; j++)
                System.out.print(alpha[j]);

            for (int k = i - 1; k >= 0; k--)
                System.out.print(alpha[k]);

            System.out.println();
        }
    }
}

How It Works

1. Outer loop grows the peak index. i runs 0..4 so peaks are A through E.

2. Forward half. Print alpha[0] through alpha[i] — the ascending side including the center.

3. Mirror without the peak. Reverse from i - 1 down to 0. Starting at i would wrongly print ABCCBA when i = 2.

Example 2 — Row Count Input

Uses characters directly (no array) and computes the peak letter for each row. Prefer hasNextInt in real apps.

Java
import java.util.Scanner;

public class PalindromePyramidInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number of rows (like 5): ");
        int n = sc.nextInt();

        for (int r = 0; r < n; r++) {
            char peak = (char) ('A' + r);

            for (char ch = 'A'; ch <= peak; ch++)
                System.out.print(ch);

            for (char ch = (char) (peak - 1); ch >= 'A'; ch--)
                System.out.print(ch);

            System.out.println();
        }

        sc.close();
    }
}

How It Works

1. Map row → peak. Row r peaks at (char)('A' + r).

2. Same up/down core. Ascend to the peak, then descend from peak - 1 — identical logic to Example 1.

3. Safer input tip. Prefer hasNextInt and stay within A–Z:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a row count from 1 to 26.");
    return;
}
int n = sc.nextInt();
if (n < 1 || n > 26) {
    System.out.println("Enter a row count from 1 to 26.");
    return;
}

Example 3 — Centered Palindrome Pyramid

Add leading spaces so shorter rows sit under the widest row (same idea as Program 16).

Java
public class PalindromePyramidCentered {
    public static void main(String[] args) {
        int n = 5;

        for (int r = 0; r < n; r++) {
            char peak = (char) ('A' + r);

            for (int s = 0; s < n - r - 1; s++)
                System.out.print(" ");

            for (char ch = 'A'; ch <= peak; ch++)
                System.out.print(ch);

            for (char ch = (char) (peak - 1); ch >= 'A'; ch--)
                System.out.print(ch);

            System.out.println();
        }
    }
}

How It Works

1. Pad first. Print n - r - 1 spaces so shorter palindromes sit under the widest row.

2. Same letter logic. Up to peak, then down from peak - 1 — unchanged from Examples 1 and 2.

3. Alignment only. Centering is layout; the palindrome rule does not change.

Edge Cases & Pitfalls

Check these before calling the solution done.

Down from peak

Doubled center

Starting the reverse loop at the peak prints ABCCBA instead of ABCBA. Always start at peak - 1.

Only up

Not a palindrome

Skipping the descending half gives Program 1 style rows (A, AB, ABC…) — not this pattern.

println early

Broken row

Call println only after both halves. Inside either loop, each letter lands on its own line.

n = 1

Single A

Output is just A; the mirror loop does not run — a good sanity check.

Past Z

Clamp to 26

More than 26 rows walks the peak past Z. Cap or reject the input.

Bad parse

Use hasNextInt

nextInt() throws on letters — check hasNextInt() so empty or non-numeric input does not crash.

Time and Space Complexity

ProgramTimeExtra space
Up/down / centered formsO(n²)O(1)

Row r (1-based) prints 2r - 1 letters. Over n rows the total is 1 + 3 + … + (2n - 1) = n².

Key Takeaways

  • Two halves: climb A..peak, then descend (peak-1)..A.
  • Center once: never start the reverse loop at the peak itself.
  • Odd lengths: each row has 2r - 1 letters; totals sum to n².
  • Complexity: O(n²) time; O(1) extra space.

One line: for each peak, print A..peak then (peak-1)..A so the row is a palindrome.

Frequently Asked Questions

The peak letter was already printed by the first loop. Starting the mirror at peak-1 avoids printing the center letter twice.
Print A..peak, then print (peak-1)..A so the center letter is not duplicated.
Row r prints 2r-1 letters. Over n rows the total is 1+3+...+(2n-1)=n².
Yes. Print each letter followed by a space in both halves, and update the sample output accordingly.
System.out.print stays on the same line for each letter. System.out.println ends the row after both halves finish.
Program 1 prints only the left half (A, AB, ABC…). This pattern mirrors back down so each full row is a palindrome.
O(n²) for n rows because each row prints O(n) characters and the sum of odd lengths is n².
Check sc.hasNextInt() before sc.nextInt(), require n ≥ 1, and cap at 26 so peaks stay within A–Z.

Did you know?

Each row prints A up to the row peak, then prints back down starting from peak - 1 so the middle letter appears only once. Row length is 2r - 1 for row r, so the total characters over n rows is n².

Next: Mirrored with Spaces

Alphabet wings separated by spaces in the middle.

Program 19 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful