Java Inverted Alphabet Triangle Pattern

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

What Is This Pattern?

An inverted alphabet right-angled triangle prints the longest letter run first, then shrinks by one letter each row — still starting from A every time.

Remember
Rule: for end letter i from last down to A,
      print A through i

ABCDE
ABCD
ABC
AB
A         ← 5 rows (longest on top)

It is the flip of Program 1: keep the same inner A..i print, reverse only the outer loop. That mirrors how star Program 2 inverts Program 1. Compare with Program 6, which shrinks from the left instead.

How to Solve It

Two ways to emit the same shape — start with nested char loops, then optionally shorten with substring.

MethodIdeaBest for
Nested char loopsOuter = shrinking end; inner = A..endLearning, interviews, exams
letters.substring(0, i)Slice shorter prefixes while i counts downShorter demos once loops click

Pseudocode

Pseudocode
for end from lastLetter down to 'A':
    for ch from 'A' to end:
        print ch (no newline)
    print newline

Cheat sheet

GoalPattern
Countdown end letterfor (char i = last; i >= 'A'; i--)
Print A..ifor (char j = 'A'; j <= i; j++) System.out.print(j);
End the rowSystem.out.println();
Top letter from rowschar top = (char)('A' + rows - 1);
One-line row shortcutSystem.out.println(letters.substring(0, i)); while i shrinks
Growing twinfor (char i = 'A'; i <= last; i++) → Program 1

Printing Letters vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach letter
System.out.printlnEnds the current lineAfter the inner loop

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

Live Preview

Change the row count and the inverted triangle updates instantly — capped at 26 letters (A–Z).

Whole numbers from 1 to 26. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 letters
ABCDE
ABCD
ABC
AB
A

Worked Walkthrough — rows = 4

Trace each outer-loop end letter as i counts down from 'D' to 'A'.

End iInner jPrinted rowLetters
'D'A..DABCD4
'C'A..CABC3
'B'A..BAB2
'A'A..AA1

Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same as Program 1, only the print order of rows differs.

Java Programs

Three complete programs: fixed top letter, Scanner input, and a substring shortcut. Use View Output to reveal sample results.

Example 1 — Fixed from 'E' down to 'A'

Hard-coded top letter — outer loop shrinks; inner loop still prints A..i.

Java
public class InvertedAlphabetTriangle {
    public static void main(String[] args) {
        for (char i = 'E'; i >= 'A'; i--) {
            for (char j = 'A'; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer loop picks the end letter. i runs from 'E' down to 'A' — longest row first.

2. Inner loop restarts at A. For each i, j runs from 'A' to i, so the row is A..i.

3. Print letters, then break the line. System.out.print(j) stays on the row; println() after the inner loop starts the next (shorter) row.

When i = 'E' you get ABCDE; when i = 'A' you get A.

Example 2 — User Input Version

Read the row count at runtime. Prefer validating with hasNextInt and clamping to 26 (shown in the tip below).

Java
import java.util.Scanner;

public class InvertedAlphabetTriangleInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
        char top = (char)('A' + rows - 1);

        for (char i = top; i >= 'A'; i--) {
            for (char j = 'A'; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }

        sc.close();
    }
}

How It Works

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

2. Map rows to a top letter. top = (char)('A' + rows - 1) — for rows = 4, top is 'D'.

3. Same shrink core. Only the source of top changes — the print logic matches Example 1.

4. Safer input tip. nextInt() throws on letters. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a whole number from 1 to 26.");
    return;
}
int rows = sc.nextInt();
if (rows < 1 || rows > 26) {
    System.out.println("Enter a whole number from 1 to 26.");
    return;
}

Example 3 — substring(0, i)

Build each shrinking prefix in one call — same shape, no explicit inner letter loop.

Java
public class InvertedAlphabetTriangleSubstring {
    public static void main(String[] args) {
        int rows = 5;
        String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (int i = rows; i >= 1; i--) {
            System.out.println(letters.substring(0, i));
        }
    }
}

How It Works

1. Count length down. i runs from rows down to 1.

2. Slice the prefix. substring(0, i) is ABCDE, then ABCD, and so on.

3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Growing triangle by mistake

If you increment i from 'A' to top, you reprint Program 1. Use i-- from top down to 'A'.

Stop at B

Missing final A

Stopping before 'A' drops the tip row. The condition must be i >= 'A'.

println inside

Column of letters

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

rows > 26

Past Z

'A' + rows - 1 leaves A–Z when rows > 26. Clamp or reject in interactive programs.

rows = 1

Single A

Output is just A — longest and shortest coincide. A good sanity check.

Bad input

Check hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require 1–26.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
Substring (Example 3)O(rows²)O(rows) per temporary row string

Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — still quadratic in n. Same totals as Program 1.

Key Takeaways

  • Rule: countdown end letter i; print A through i each row.
  • Flip of Program 1: same inner loop — only reverse the outer loop.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time from the triangular letter count; O(1) extra space for nested loops.

One line: for i from the top letter down to 'A', print A through i, then println.

Frequently Asked Questions

The outer loop counts down so the first row prints the most letters and each next row prints one fewer. The inner loop still prints from A up to the current bound.
Because the inner loop always starts at A. That resets the sequence each row, producing ABCDE then ABCD and so on.
Program 1 grows row length (A, AB, ABC). This pattern shrinks it (ABCDE, ABCD, ABC). Only the outer loop direction changes; the inner loop is the same A..i print.
Program 4 grows while printing backward (A, BA, CBA). This pattern shrinks while printing forward from A (ABCDE, ABCD, ABC).
System.out.print stays on the same line. System.out.println ends the current line. Letters use print; the row break uses println after the inner loop.
O(n²) for n rows, because total printed characters are n(n+1)/2.
Yes. Keep a string of A–Z and print letters.substring(0, i) while i counts down from rows to 1. Nested char loops are better for learning; substring is a handy shortcut later.
Check sc.hasNextInt() before sc.nextInt(), require n ≥ 1, and cap at 26 so bad input does not walk past Z.

Did you know?

This is the inverted twin of Program 1: same inner A..i print, only the outer loop shrinks. For 5 rows: ABCDE, ABCD, ABC, AB, A. Total letters still equal n(n+1)/2.

Next: Increasing Start Letter

Keep a fixed end letter and move the start letter right each row.

Program 6 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