Right-Aligned Alphabet Pyramid in Java

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

What You’ll Learn

We use a simple trick: print a shrinking number of leading spaces, then print letters starting from A up to the current row letter.

Because letters use %2c, use a monospace font if you want the right edge to look perfect.

⭐ Pattern Output

Output for 5 rows:

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

Java Program (Fixed A–E)

Fixed range A.. E, with one-space padding and %2c letters.

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

🧠 How It Works

1

Outer i

Sets the last letter for the current row: A through E.

Row
2

Padding loop

Print one space while j > i. That is 'E' - i spaces, shrinking by 1 each row.

Space
3

Letter loop

Print A.. i using System.out.format(\"%2c\", ...) so the sample spacing matches.

A..i
4

Constant visual width

Padding shrinks as the letter run grows, so the right margin stays aligned when printed in a monospace font.

Align
=

Example row

At i = 'C', two padding spaces then three %2c letters. Five rows × O(n) inner work → O(n²) for n letters.

2

Variation — User Input

Let the user choose the number of rows. We compute endChar as 'A' + rows - 1, clamp to 1–26, and reuse the same loop structure.

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 = endChar; j > i; j--) {
                System.out.print(' ');
            }
            for (char k = startChar; k <= i; k++) {
                System.out.format(\"%2c\", k);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Left-align the pattern by removing the padding loop
  • Use \"%3c\" and adjust padding if you want wider columns
  • Validate rows to keep output within AZ

Avoid

  • Expecting alignment in a proportional font
  • Mixing double-space padding and %2c width without checking spacing

Key Takeaways

1

Padding count per row is endChar - i spaces.

2

Letters per row is i - startChar + 1.

3

%2c matches the spaced sample output.

4

O(n²) work for n rows.

❓ Frequently Asked Questions

When j == i, we want to stop padding and begin printing letters, so the condition is strictly j > i.
Yes—character literals are clearer and compile to the same integer values. The variation example uses the char-literal style.
O(n²) for n rows.

More Java alphabet patterns

Once you understand padding and loop bounds, you can build many aligned triangles and pyramids.

All Alphabet Patterns →
Did you know?

Each step down removes one padding space and adds one more letter, so the right edge stays fixed when printed in a monospace font.

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