Right-Aligned Sequential Pyramid in Java

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

What You’ll Learn

This pattern prints letters in a running sequence (A, then B C, then D E F…) but keeps the triangle right-aligned by printing empty cells first.

For best alignment, view output in a monospace terminal because we rely on fixed-width cells.

⭐ Pattern Output

Output for 5 rows:

Output
        A
      B C
    D E F
  G H I J
K L M N O
1

Java Program (Fixed 5 Rows)

Direct adaptation of the reference logic using ASCII values and System.out.format("%2c", k++) for aligned columns.

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

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

🧠 How It Works

1

k streams the alphabet

k starts at 65 (A) and only advances when a letter is actually printed via System.out.format("%2c", k++), so the triangle reads as one continuous sequence laid into a fixed grid.

Counter
2

Outer i sets how many letters appear

i walks row codes 6569. Row i exposes i - 64 letter slots; the remaining columns on the line stay padded.

Rows
3

Inner j scans columns high → low

For each j from 69 down to 65, if j > i print two spaces; else print the next streamed letter in a width-two field. That keeps columns aligned in a monospace font.

Alignment
4

Why the inner loop is always five wide

You always iterate across the full code range for the pattern, so each row has five logical columns. Padding plus %2c preserves the staircase shape.

Grid
=

Letter count

1+2+3+4+5 = 15 letters through O. Padding adds more writes; time still scales as O(n²) for n rows in this fixed-grid style.

2

Variation — User Input

Choose the number of rows and compute the last character for both loops. 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 base = 65;
        int last = base + rows - 1;
        int k = base;

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

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Switch to char loops ('A'…)
  • Add a space after each printed letter for readability (adjust padding to match)
  • Validate input so rows stays in 1–26

Avoid

  • Changing padding width without updating %2c (misalignment)
  • Resetting k each row (sequence breaks)

Key Takeaways

1

Use one running counter (k++) to print sequential letters.

2

Keep padding width equal to the letter cell width (%2c).

3

Total letters for n rows is n(n+1)/2 (triangular number).

4

Overall complexity is O(n²).

❓ Frequently Asked Questions

System.out.format uses java.util.Formatter: %2c prints a character right-aligned in a field of width 2 (padded with spaces).
Yes—for example System.out.print(" "); System.out.print((char) (k++));—but then padding must also be one cell wide to keep alignment consistent.
Alignment depends on equal character widths. In proportional fonts, spaces and letters have different widths so columns won’t line up.
The output will go beyond Z unless you limit rows to 26 or change the character set.

More Java alphabet patterns

Right alignment is easiest when you treat each printed item as a fixed-width cell.

All Alphabet Patterns →
Did you know?

The total number of printed letters after n rows is the nth triangular number: 1 + 2 + … + n = n(n+1)/2.

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