Java Alphabet Pyramid Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned alphabet pyramid prints growing prefixes of the alphabet (A, A B, A B C, …) pushed right with leading spaces so every row shares the same right edge.

Remember
Rule: for row letter i from 'A' to top,
      print (top - i) spaces, then A..i (width 2)

    A
   A B
  A B C
 A B C D
A B C D E     ← top = 'E'

Unlike Program 22, letters restart from A on every row. Unlike Program 16, this only right-aligns — it does not fully center the pyramid.

How to Solve It

Two ways to emit the same shape — char loops with pads, then optionally rewrite with int row indexes.

MethodIdeaBest for
Char loopsPad top..i+1; print A..i with %2cLearning, interviews, exams
Int row indexPad n - row; print row letters from AWhen you think in row numbers

Pseudocode

Pseudocode
for i from 'A' to top:
    for j from top down to (i + 1):
        print " "
    for k from 'A' to i:
        print k in width-2 field
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (char i = 'A'; i <= top; i++)
Leading spacesfor (char j = top; j > i; j--) System.out.print(" ");
Prefix lettersfor (char k = 'A'; k <= i; k++) System.out.printf("%2c", k);
End the rowSystem.out.println();
Pad counttop - i (4 when i is A and top is E)
Restart lettersAlways start the letter loop at 'A'
Continuous twinProgram 22 (k++ stream)

Printing Letters vs Starting a New Line

APIEffectUse for
System.out.print / printfStays on the same lineEach pad space and each letter
System.out.printlnEnds the current lineAfter both inner loops

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

Live Preview

Change the top letter and the right-aligned pyramid updates instantly — including the letter total.

One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.

Live result Top E · 5 rows · 15 letters
     A
    A B
   A B C
  A B C D
 A B C D E

Worked Walkthrough — Top = E

Trace each row’s pad count, letter prefix, and printed line (letters use width-2 cells).

iPad spacesLettersPrinted row
A4AA
B3A BA B
C2A B CA B C
D1A B C DA B C D
E0A B C D EA B C D E

Pad count = top - i. Letters always restart at A. Total letters: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.

Java Programs

Three complete programs: fixed A–E, top-letter input, and an int-index rewrite. Use View Output to reveal sample results.

Example 1 — Fixed A–E

First print leading spaces, then print letters A..i using %2c formatting.

Java
public class RightAlignedPrefixPyramid {
    public static void main(String[] args) {
        for (char i = 'A'; i <= 'E'; i++) {
            for (char j = 'E'; j > i; j--)
                System.out.print(" ");

            for (char k = 'A'; k <= i; k++)
                System.out.printf("%2c", k);

            System.out.println();
        }
    }
}

How It Works

1. Outer loop picks the row end. i runs from 'A' to 'E' — one growing prefix per row.

2. Pad first. Print one space for each j from 'E' down while j > i.

3. Restart letters. Print k from 'A' through i with %2c.

4. Break the line. System.out.println() after both inner loops starts the next row.

When i = 'C', two leading spaces print, then letters A B C.

Example 2 — Top Letter Input

The pattern prints up to that row. Prefer validating a single A–Z character in real apps.

Java
import java.util.Scanner;

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

        System.out.print("Enter top letter (like E): ");
        char top = sc.next().toUpperCase().charAt(0);

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

            for (char k = 'A'; k <= i; k++)
                System.out.printf("%2c", k);

            System.out.println();
        }

        sc.close();
    }
}

How It Works

1. Prompt and read. Ask for a top letter, then take the first character (uppercased).

2. Same pad + prefix core. Pad count is always top - i; only the shared top letter changes.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNext()) {
    System.out.println("Enter one letter from A to Z.");
    return;
}
String raw = sc.next().trim().toUpperCase();
if (raw.length() != 1 || raw.charAt(0) < 'A' || raw.charAt(0) > 'Z') {
    System.out.println("Enter one letter from A to Z.");
    return;
}
char top = raw.charAt(0);

Example 3 — Int Row Index

Often clearer if you think in row numbers: pad n - row spaces, then print row letters from A.

Java
public class RightAlignedPrefixPyramidInt {
    public static void main(String[] args) {
        char top = 'E';
        int n = top - 'A' + 1;

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

            for (int L = 0; L < row; L++)
                System.out.printf("%2c", (char) ('A' + L));

            System.out.println();
        }
    }
}

How It Works

1. Derive height. n = top - 'A' + 1 is 5 when top is 'E'.

2. Pad by row number. Row 1 pads n - 1 spaces; the last row pads 0.

3. Letters from A. Letter L is (char)('A' + L) — row 1 prints A; row 5 prints A..E.

Edge Cases & Pitfalls

Check these before calling the solution done.

k++

Continuous stream by mistake

If you keep a running k++ across rows, you get Program 22-style output. Restart the letter loop at 'A' each row.

No pads

Left-aligned instead

Skipping the space loop prints a left-aligned triangle. Keep j from top down while j > i.

println inside

Broken outline

If println is inside either inner loop, each cell lands on its own line. Use print/printf for pads and letters; println only after both loops.

top = A

Single letter

Output is just a width-2 A cell. Pad loop never runs. A good sanity check.

top < A

Empty output

Outer loop never runs if top < 'A'. Validate A–Z before looping.

Bad input

Validate one letter

Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.

Time and Space Complexity

ProgramTimeExtra space
Char loops (Examples 1–2)O(n²)O(1)
Int row index (Example 3)O(n²)O(1)

For n = top - 'A' + 1 rows, each row prints up to O(n) spaces plus O(n) letters. Letter total = 1 + 2 + … + n = n(n + 1)/2.

Key Takeaways

  • Rule: for row i, print top - i spaces, then letters A..i.
  • Restart A: every row begins the letter loop at 'A' — not a running k++.
  • Break the row: call println only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row letter i from A to top, print top - i spaces, then A..i with width 2, then println().

Frequently Asked Questions

The first inner loop prints (E - i) leading spaces, fewer on each lower row, so the letter block ends at the same right edge.
Width-2 formatting prints each letter in an even column so the output matches the spaced layout (best viewed in a monospace font).
Program 22 uses a continuous k++ stream across all rows. Program 27 restarts letters from A on every row and uses leading spaces to push rows to the right.
Remove the leading-space loop and print letters directly from A to the row letter.
O(n²) for n rows because each row prints O(n) spaces plus O(n) letters.
Use Scanner next(), take charAt(0) after toUpperCase(), require A–Z, and reject empty or multi-character tokens.
Program 16 centers the pyramid with more padding. This pattern only right-aligns by shrinking leading spaces while restarting A..i each row.
Yes. Pad n − row spaces, then print row letters as (char)('A' + L). Example 3 on this page shows that style.

Did you know?

Leading padding: for each row letter i, the loop prints E - i spaces. Then the letter loop prints A through i using %2c so columns look even in monospace output. The last row has no padding; all rows share the same right edge.

Next: Symmetric Square

Move from a growing prefix pyramid to a concentric decreasing alphabet square.

Program 28 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