Right-Aligned Reverse Alphabet Pyramid in Java

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

What You’ll Learn

This is a companion to program 22: it uses the same fixed-width grid (two spaces for padding + %2c for letters), but prints a reverse alphabet slice on each row.

Use a monospace font in your terminal so columns stay even.

⭐ Pattern Output

Output for 5 rows (E down to A):

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

Java Program (Fixed A–E)

First print padding pairs, then print letters from E down to the current row letter.

Java
public class Main {
    public static void main(String[] args) {
        int i, j;

        for (i = 69; i >= 65; i--) {
            for (j = 65; j < i; j++) {
                System.out.print("  ");
            }
            for (j = 69; j >= i; j--) {
                System.out.format("%2c", j);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Outer loop i (E → A)

Top row prints 1 letter (E), bottom row prints 5 letters (E D C B A).

Rows
2

Padding loop j = A .. i-1

Each iteration prints " ". As i decreases, padding shrinks (four pairs down to zero).

Align
3

Letter loop j = E .. i

System.out.format("%2c", j) gives each letter a 2-column cell to match the two-space padding.

%2c
=

Quick check

At i = C (67), you get two padding pairs and then E D C.

2

Variation — User Input

Compute end as 'A' + rows - 1, then run the same two-loop structure. 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));
        final int start = 'A';
        int end = start + rows - 1;

        for (int i = end; i >= start; i--) {
            for (int j = start; j < i; j++) {
                System.out.print("  ");
            }
            for (int j = end; j >= i; j--) {
                System.out.format("%2c", j);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Switch to character loops (for (char i = (char) end; i >= 'A'; i--))
  • Use lowercase by starting at 'a' + offset
  • Validate input so rows stays in 1–26

Avoid

  • Changing padding width without matching the %2c field width
  • Running past Z without accounting for non-letter characters

Key Takeaways

1

Use a dedicated padding loop to right-align the triangle.

2

The letter loop prints end down to i each row (a reverse slice).

3

%2c + " " keeps a consistent grid in the console.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

Padding shifts the visible letters to the right. Without it, the pyramid would be left-aligned.
Yes—character literals are clearer and behave the same (they compile to the same integer codes).
O(n²) for n rows.

More Java alphabet patterns

Splitting padding and content into two loops is a clean way to build aligned console pyramids.

All Alphabet Patterns →
Did you know?

With 5 rows, you still print 1 + 2 + 3 + 4 + 5 = 15 letters in total—same count as program 22, just arranged differently.

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