Java Sequential Alphabet Triangle Pattern (Decreasing)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A sequential decreasing alphabet triangle prints one continuous letter stream across rows that shrink by one letter each time — never restarting at A.

Remember
Rule: keep k at 'A' outside the outer loop;
      row i prints (n - i + 1) letters from k++

A B C D E
F G H I
J K L
M N
O         ← 5 rows (15 letters, A through O)

Companion to Program 22: same continuous k++ stream, but this pattern shrinks each row and stays left-aligned instead of growing with pad cells.

How to Solve It

Two ways to emit the same stream — countdown inner bounds, or an explicit letters-per-row count.

MethodIdeaBest for
Countdown innerj from n down to i; print k++Learning, interviews, exams
Explicit countcount = n - i + 1; loop count timesWhen the shrink rule should read clearly

Pseudocode

Pseudocode
k = 'A'
for i from 1 to n:
    for j from n down to i:
        print k; k++
    print newline

Cheat sheet

GoalPattern
Shared streamchar k = 'A'; outside the outer loop
Walk each rowfor (int i = 1; i <= n; i++)
Shrink widthfor (int j = n; j >= i; j--)
Print next letterSystem.out.printf("%2c ", k++);
End the rowSystem.out.println();
Letters on row in - i + 1
Growing twinProgram 22 (right-aligned k++)

Printing Letters vs Starting a New Line

APIEffectUse for
System.out.print / printfStays on the same lineEach letter (and its spacing)
System.out.printlnEnds the current lineAfter the inner loop

Print letters without a newline, then end the row once.

Live Preview

Change the height and the shrinking stream updates instantly — including the letter total.

Whole numbers from 1 to 6 so the stream stays in A–Z (n(n+1)/2 ≤ 21 for n = 6).

Live result 5 rows · 15 letters
A B C D E
F G H I
J K L
M N
O

Worked Walkthrough — 5 rows

Trace each row’s length and the letters taken from the running counter.

iLettersCountPrinted row
1A..E5A B C D E
2F..I4F G H I
3J..L3J K L
4M..N2M N
5O1O

Total letters: 5 + 4 + 3 + 2 + 1 = 15 = 5×6/2 (A through O). That triangular sum is why time is O(n²).

Java Programs

Three complete programs: fixed 5 rows, Scanner input, and an explicit count-per-row rewrite. Use View Output to reveal sample results.

Example 1 — Fixed 5 Rows

One counter k supplies letters; the inner loop decides how many times to print it in each row.

Java
class SequentialDecreasing {
    public static void main(String[] args) {
        char k = 'A';

        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--)
                System.out.printf("%2c ", k++);

            System.out.println();
        }
    }
}

How It Works

1. Start the stream. k = 'A' lives outside the outer loop so it never resets.

2. Outer loop picks the row. i runs from 1 to 5.

3. Inner loop shrinks. j runs from 5 down to i — five letters, then four, then three, …

4. Print and advance. System.out.printf("%2c ", k++) prints the next letter in a width-2 cell, then increments k.

When i = 3, the inner loop runs three times and prints J K L; the next row continues at M.

Example 2 — Row Count Input

If rows are large, letters go past Z unless you wrap or cap. Prefer hasNextInt() and a letter-budget check.

Java
import java.util.Scanner;

class SequentialDecreasingInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows (like 5): ");
        int n = sc.nextInt();
        if (n < 1) return;

        char k = 'A';
        for (int i = 1; i <= n; i++) {
            for (int j = n; j >= i; j--)
                System.out.printf("%2c ", k++);

            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and read. Ask for a row count, then read an int with Scanner.

2. Same stream and shrink. Only the shared width follows n — letter count is n(n+1)/2.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive whole number.");
    return;
}
int n = sc.nextInt();
if (n < 1) {
    System.out.println("Enter a positive whole number.");
    return;
}
if (n * (n + 1) / 2 > 26) {
    System.out.println("Too many letters for A–Z. Use a smaller n.");
    return;
}

Example 3 — Explicit Count Per Row

Often clearer to read: print n - i + 1 letters from the shared stream.

Java
class SequentialDecreasingCount {
    public static void main(String[] args) {
        int n = 5;
        char k = 'A';

        for (int i = 1; i <= n; i++) {
            int count = n - i + 1;
            for (int t = 0; t < count; t++)
                System.out.print(k++ + " ");

            System.out.println();
        }
    }
}

How It Works

1. Same continuous stream. k still starts at 'A' outside the outer loop.

2. Name the width. count = n - i + 1 makes the shrink rule obvious — row 1 prints 5 letters; row 5 prints 1.

3. Simple spacing. k++ + " " skips width-2 formatting and adds a plain space after each letter.

Edge Cases & Pitfalls

Check these before calling the solution done.

Reset k

Every row starts at A

If you put k = 'A' inside the outer loop, you lose the continuous stream. Keep k outside.

j = 1..i

Growing triangle by mistake

An ascending inner bound reprints a growing shape. Use j from n down to i (or count = n - i + 1).

println inside

Column of letters

If println is inside the inner loop, each letter lands on its own line. Use print/printf for letters; println only after the inner loop.

n = 1

Single A

Output is just A. A good sanity check.

Past Z

Letter budget

Total letters = n(n+1)/2. For A–Z only, keep that ≤ 26 (so n ≤ 6 without wrapping).

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require n ≥ 1.

Time and Space Complexity

ProgramTimeExtra space
Countdown inner (Examples 1–2)O(n²)O(1)
Explicit count (Example 3)O(n²)O(1)

Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — still quadratic in n. Same totals as an inverted triangle of stars; only the printed symbols differ.

Key Takeaways

  • Rule: keep k outside; row i prints n - i + 1 letters from k++.
  • Never reset: putting k = 'A' inside the outer loop restarts every row.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time from the triangular letter count; O(1) extra space.

One line: for each row i, print n - i + 1 letters from the shared k++ stream, then call println().

Frequently Asked Questions

Because the pattern is one continuous alphabet stream A through O. Resetting k would restart each row from A.
So the sequence continues across rows (A, B, C, ...). If you reset k each row, every row would start at A again.
For the fixed 5-row example, the row lengths are 5, 4, 3, 2, 1. Each new row prints one fewer letter.
The width-2 formatting aligns the columns (best in a monospace terminal). Adding a trailing space makes the output match the sample with spaces between letters.
Build a row string and trim it, or print a space only between letters (not after the last letter in the row).
Both use a continuous k++ stream. Program 22 grows rows and right-aligns with pad cells; this pattern shrinks row length each time and stays left-aligned.
O(n²) for n rows because total prints are 1+2+...+n = n(n+1)/2.
Check sc.hasNextInt() before sc.nextInt(), require n ≥ 1, and cap so n(n+1)/2 ≤ 26 if you want only A–Z letters.

Did you know?

One counter k starts at A and never resets. The outer loop controls row count; the inner loop length decreases each row (5, 4, 3, 2, 1). Width-2 formatting (or a trailing space) keeps a clean column layout in monospace output.

Next: Rotating Alphabet

Move from a shrinking stream to circular letter shifts (ABCDE, BCDEA, …).

Program 26 tutorial →

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