Palindromic Alphabet Pyramid in Java

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

Each row is a palindrome built from two parts: a descending run (i down to B) and an ascending run (A up to i).

This program prints letters with no extra spaces (unlike right-aligned patterns that use a fixed-width grid). Compare program 18 (palindrome with k = i - 1 on one continuous pass).

⭐ Pattern Output

Output for 5 rows:

Output
A
BAB
CBABC
DCBABCD
EDCBABCDE
1

Java Program (Fixed A–E)

Same logic as the reference: two loops per row to build the palindrome.

Java
public class Main {
    public static void main(String[] args) {
        int i, j;
        for (i = 65; i <= 69; i++) {
            for (j = i; j > 65; j--) {
                System.out.print((char) j);
            }
            for (j = 65; j <= i; j++) {
                System.out.print((char) j);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Outer i is the peak letter (ASCII)

i runs 6569 (AE). That value is both the highest letter on the row and the pivot between the descending and ascending halves.

Grow
2

Left wing: j from i down while j > 65

System.out.print((char) j) prints i, then i-1, … down to B. Stopping above 65 skips the first A so it can appear once in the next loop.

Desc
3

Right wing: j from 65 up to i

The second loop prints A through i inclusive. Together with the left wing you get an odd-length palindrome such as CBABC when i == 'C'.

Asc
4

First row edge case

When i == 65, the descending loop body never runs; the ascending loop prints only A. That matches the single-letter first line of the sample output.

A
=

Odd-length rows

Row length is 2 × (i - 65) + 1. Summing over n rows gives O(n²) characters from two inner passes per outer iteration.

2

Variation — User Input

Read rows and compute endChar as 'A' + rows - 1. Clamp rows to 1–26.

Java
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 = i; j > startChar; j--) {
                System.out.print(j);
            }
            for (char j = startChar; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Insert spaces between letters (then update the output preview)
  • Center the pyramid by printing leading spaces based on the widest row
  • Validate rows so endChar stays within AZ

Avoid

  • Using j >= 'A' in the descending loop without removing A from the ascending loop
  • Letting rows push output past Z without handling it

Key Takeaways

1

The descending loop stops before A, so the center character is printed once.

2

Each row is an odd-length palindrome: lengths 1, 3, 5, …

3

Total characters printed across n rows is .

4

O(n²) time for n rows.

❓ Frequently Asked Questions

Because the forward loop already prints A. Stopping early prevents a double A in the middle.
Add a leading-space loop before printing letters: for row index r (0-based), print (n - r - 1) spaces first.
O(n²) because the total output size is characters.

More Java alphabet patterns

Palindromic rows are a great way to practice nested loops and loop boundaries.

All Alphabet Patterns →
Did you know?

The first n odd numbers sum to , which matches the total characters printed across n rows in this pattern.

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