Reverse Centered Alphabet Pyramid in Java

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

What You’ll Learn

This reverse centered alphabet pyramid reuses the row logic from program 28: print downward from E to A, then upward from B to E so the center row is not repeated.

Each cell still uses the same rule: if j > i, print j, else print i.

⭐ Pattern Output

Nine rows for AE (space after each letter):

Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E
1

Java Program (Fixed A–E)

Two phases share the same two inner loops; the second phase starts at B to skip the duplicate center.

Java
public class Main {
    public static void main(String[] args) {
        int i, j;
        int k = 69; // 'E'

        // Upper half (E -> A)
        for (i = k; i >= 65; i--) {
            for (j = k; j >= 65; j--) {
                if (j > i) System.out.print((char) j + \" \");
                else System.out.print((char) i + \" \");
            }
            for (j = 66; j <= k; j++) {
                if (j > i) System.out.print((char) j + \" \");
                else System.out.print((char) i + \" \");
            }
            System.out.println();
        }

        // Lower half (B -> E)
        for (i = 66; i <= k; i++) {
            for (j = k; j >= 65; j--) {
                if (j > i) System.out.print((char) j + \" \");
                else System.out.print((char) i + \" \");
            }
            for (j = 66; j <= k; j++) {
                if (j > i) System.out.print((char) j + \" \");
                else System.out.print((char) i + \" \");
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Upper half then lower half

First outer loop prints E down to A. Second outer loop prints B back up to E.

2 phases
2

Two symmetric scans per row

Each row prints a left half (EA) then a right half (BE) to stay symmetric.

Mirror
3

One rule builds the shells

If j > i print j else print i. That keeps higher letters on the outside.

j>i
4

Why start the lower half at B?

The center row with A is already printed in the upper half. Starting at B avoids duplicating it.

No dup
=

9-by-9 output grid

For 5 letters (AE), total rows are 2n - 1 = 9, each row nine letters wide.

2

Variation — User Input

Generalize by choosing the top letter with rows (clamped 1–26). Upper half: top down to A. Lower half: B up to top.

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 top = (char) ('A' + rows - 1);

        for (char i = top; i >= 'A'; i--) {
            for (char j = top; j >= 'A'; j--) System.out.print((j > i ? j : i) + " ");
            for (char j = 'B'; j <= top; j++) System.out.print((j > i ? j : i) + " ");
            System.out.println();
        }

        for (char i = 'B'; i <= top; i++) {
            for (char j = top; j >= 'A'; j--) System.out.print((j > i ? j : i) + " ");
            for (char j = 'B'; j <= top; j++) System.out.print((j > i ? j : i) + " ");
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Remove the trailing space after the last character on each row
  • Try a numeric version using the same shells logic
  • Switch to lowercase by starting from 'a'

Avoid

  • Starting the lower half from A (duplicates the center row)
  • Letting rows exceed 26 without handling non-letter output

Key Takeaways

1

Two phases move the floor letter from the outer edge to A and back, using the same row rule.

2

Lower half starts at B to avoid duplicating the center row.

3

Each cell uses the same rule: print j if j > i, else print i.

4

O(n²) total work for n letters.

❓ Frequently Asked Questions

The upper half already includes the A-centered row. Starting at B prevents a duplicate center line.
For n letters from A to the top letter, total rows are 2n - 1.
O(n²) because there are O(n) rows and each row prints O(n) cells.

More Java alphabet patterns

Once you can build one phase cleanly, repeating it in reverse is an easy way to close reverse centered pyramids and other symmetric grids.

All Alphabet Patterns →
Did you know?

Many symmetric block patterns are an upper part plus the same logic repeated in reverse, with the middle row printed only once.

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