Sequential Decreasing Alphabet Triangle in Java

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

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:

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

Java Program (Fixed A–E)

One counter k supplies letters; the inner loop only decides how many times to print.

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 >= i; j--) {
                System.out.printf("%2c", k++);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

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.

Stream
2

Outer i picks the row’s low letter

i runs 6569 (AE). It is the smallest index printed on that row; the inner loop stops when j < i.

Rows
3

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.

Count
4

%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++).

Align
=

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.

2

Variation — User Input

Read rows, derive endChar, clamp to 1–26, and keep k running forward.

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);
        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 k at 'a' for lowercase
  • Cap rows so the stream does not pass Z unless you wrap or extend the alphabet

Avoid

  • Resetting k each row (breaks the continuous sequence)
  • Changing the width format without checking alignment in the terminal

Key Takeaways

1

Keep one running k++ so letters continue across rows.

2

Row length decreases by 1 each line.

3

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

4

O(n²) output size for n rows.

❓ Frequently Asked Questions

Because the pattern continues A, B, C… across the whole triangle.
Yes—System.out.format("%2c", k++) behaves the same for this use case (both use java.util.Formatter).
O(n²) because total printed letters grow quadratically with n.

More Java alphabet patterns

Running counters plus inner loop counts are a common interview-style loop exercise.

All Alphabet Patterns →
Did you know?

For n rows, total printed letters are n(n+1)/2. For n = 5, that’s 15 letters, ending at O.

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