Sequential Decreasing Alphabet Triangle in Java

What You’ll Learn
This pattern prints a continuous sequence of letters across rows, but each new row prints one fewer character than the previous.
We use System.out.printf("%2c", k++) so spacing matches the original System.out.format("%2c", …) (best viewed in a monospace terminal).
⭐ Pattern Output
Output for 5 rows:
A B C D E
F G H I
J K L
M N
OJava Program (Fixed A–E)
One counter k supplies letters; the inner loop only decides how many times to print.
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 >= i; j--) {
System.out.printf("%2c", k++);
}
System.out.println();
}
}
}🧠 How It Works
k walks forward through the alphabet
Initialize k = 65 (A). Each output uses System.out.printf("%2c", k++) so the triangle is one continuous stream rather than restarting every row.
Outer i picks the row’s low letter
i runs 65…69 (A…E). It is the smallest index printed on that row; the inner loop stops when j < i.
Inner j from E down to i
Counting down from 69 to i yields shorter rows as i rises: widths 5, 4, 3, 2, 1.
%2c for alignment
A width of two adds leading space before single letters so columns line up like the sample (same idea as setw(2) in C++).
Triangular total
5 + 4 + 3 + 2 + 1 = 15 letters through O. Total prints for n rows are n(n+1)/2, so the nested structure is O(n²) in output size.
Variation — User Input
Read rows, derive endChar, clamp to 1–26, and keep k running forward.
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);
int k = 'A';
for (char i = startChar; i <= endChar; i++) {
for (char j = endChar; j >= i; j--) {
System.out.printf("%2c", k++);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Use
"%3c"for wider spacing - Start
kat'a'for lowercase - Cap
rowsso the stream does not passZunless you wrap or extend the alphabet
Avoid
- Resetting
keach row (breaks the continuous sequence) - Changing the width format without checking alignment in the terminal
Key Takeaways
Keep one running k++ so letters continue across rows.
Row length decreases by 1 each line.
Total letters for n rows is n(n+1)/2.
O(n²) output size for n rows.
❓ Frequently Asked Questions
A, B, C… across the whole triangle.System.out.format("%2c", k++) behaves the same for this use case (both use java.util.Formatter).More Java alphabet patterns
Running counters plus inner loop counts are a common interview-style loop exercise.
For n rows, total printed letters are n(n+1)/2. For n = 5, that’s 15 letters, ending at O.
12 people found this page helpful
