Centered Alphabet Pyramid in Java

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

What You’ll Learn

Three rows, odd-width letter groups, padded with spaces so the block lines up like a pyramid. Letters flow in order through one counter k.

Related: program 14 (odd-width triangle without centering) and Java star pattern 1 (spaces + symbols).

⭐ Pattern Output

Three rows (spaces between letters; leading spaces align columns):

Output
    A
  B C D
E F G H I
1

Complete Java Program (Three Rows)

Bottom row ends at 'E' (69); outer i runs 65, 67, 69. Inner j runs 69 down to 65.

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

🧠 How It Works

1

k = 65

Continuous alphabet index; not reset each row.

Stream
2

Outer i += 2

Row “cap” letters: A, C, E for this sample.

Rows
3

Inner scan j = 69 .. 65

If j > i, print padding spaces; otherwise print the next alphabet character from k.

Grid
4

First row detail

For i = 65, values j = 69, 68, 67, 66 satisfy j > i (four spaces); j = 65 prints A.

Center
=

Work per row

This sample always runs five inner steps; for general rows, the inner width is 2 * rows - 1 positions per line, so total work is O(r²).

2

Variation — User Input

Last outer i is base + 2*(rows-1); inner j starts at the same value. Same j > i rule. Clamp rows to 1–5 so k stays within A–Z (total letters printed = rows²).

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 (max 5): ");
        int rows = sc.nextInt();

        rows = Math.max(1, Math.min(rows, 5));
        final int base = 65;
        int last = base + 2 * (rows - 1);
        int i, j;
        int k = 65;

        for (i = base; i <= last; i += 2) {
            for (j = last; j >= base; j--) {
                if (j > i) {
                    System.out.print(" ");
                } else {
                    System.out.print((char) (k++) + " ");
                }
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Rewrite with character literals: for (i = 'A'; i <= 'E'; i += 2) and matching j bounds
  • Two-loop style: print (last - i) / 2 spaces, then a dedicated letter loop (same geometry, clearer intent)
  • Clamp rows so k does not pass 'Z'

Avoid

  • Confusing j > i with “odd-only j” — every integer j between bounds is visited
  • Resetting k each row (you would repeat A every line)

Key Takeaways

1

Fixed-width inner scan + j > i produces leading padding.

2

k++ only on printed letters keeps the alphabet continuous.

3

Outer step 2 gives 1, 3, 5, … letters per row.

4

O(r²) for r rows with width scaling with r.

❓ Frequently Asked Questions

For i = 65, values j = 69, 68, 67, 66 satisfy j > i, so you print four spaces; j = 65 prints the letter.
base + 2*(rows-1) is the ASCII code of the last row’s end letter. When rows is 3 that is 69 ('E').
Print (char)(k++) without the trailing " ". Centering logic is unchanged; only the letter spacing tightens.
O(r²) for r rows in the generalized version.

More Java alphabet patterns

Centering is often “print padding, then print the payload” — here both share one inner loop.

All Alphabet Patterns →
Did you know?

When tracing row 1, list every j from 69 down to 66 — each is > 65, so you get four padding spaces.

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