Right-Aligned Sequential Pyramid in Java

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:
A
B C
D E F
G H I J
K L M N OJava Program (Fixed 5 Rows)
Direct adaptation of the reference logic using ASCII values and System.out.format("%2c", k++) for aligned columns.
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
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.
Outer i sets how many letters appear
i walks row codes 65…69. Row i exposes i - 64 letter slots; the remaining columns on the line stay padded.
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.
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.
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.
Variation — User Input
Choose the number of rows and compute the last character for both loops. Clamp rows to 1–26.
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
charloops ('A'…) - Add a space after each printed letter for readability (adjust padding to match)
- Validate input so
rowsstays in 1–26
Avoid
- Changing padding width without updating
%2c(misalignment) - Resetting
keach row (sequence breaks)
Key Takeaways
Use one running counter (k++) to print sequential letters.
Keep padding width equal to the letter cell width (%2c).
Total letters for n rows is n(n+1)/2 (triangular number).
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).System.out.print(" "); System.out.print((char) (k++));—but then padding must also be one cell wide to keep alignment consistent.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.
The total number of printed letters after n rows is the nth triangular number: 1 + 2 + … + n = n(n+1)/2.
12 people found this page helpful
