Java Alphabet Triangle Pattern (Reverse Order)

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

What Is This Pattern?

A reverse-order alphabet triangle prints a left-aligned staircase where each row’s highest letter grows forward, but letters along the row print backward down to A.

Remember
Rule: on row with peak i, print i down to A

A
BA
CBA
DCBA
EDCBA     ← 5 rows

It is the row-order flip of Program 1: same growing widths, but the inner loop counts down instead of up. Compare with Program 3, which changes the start letter while still printing forward.

How to Solve It

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

MethodIdeaBest for
Nested char loopsOuter = peak letter; inner = peak down to ALearning, interviews, exams
Reverse substringSlice A..peak, reverse, print onceShorter demos once loops click

Pseudocode

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

Cheat sheet

GoalPattern
Walk each peakfor (char i = 'A'; i <= last; i++)
Print i..Afor (char j = i; j >= 'A'; j--) System.out.print(j);
End the rowSystem.out.println();
Last letter from rowschar last = (char)('A' + rows - 1);
One-line reverse shortcutReverse letters.substring(0, i) then println
Forward twinProgram 1 (j from A up to i)

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 reverse-order 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
A
BA
CBA
DCBA
EDCBA

Worked Walkthrough — rows = 4

Trace each outer-loop peak letter and count how many times the inner loop prints.

Peak iInner jPrinted rowLetters
'A'A..AA1
'B'B..ABA2
'C'C..ACBA3
'D'D..ADCBA4

Total letter prints: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).

Java Programs

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

Example 1 — Fixed through 'E'

Hard-coded peak letter — outer grows A..E; inner counts down to A.

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

How It Works

1. Outer loop picks the peak. i runs from 'A' to 'E' — one row per peak letter.

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

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

When i = 'A' you get A; when i = 'B' you get BA; up through EDCBA.

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 ReverseOrderTriangleInput {
    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 last = (char)('A' + rows - 1);

        for (char i = 'A'; i <= last; i++) {
            for (char j = i; j >= 'A'; 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 last peak. last = (char)('A' + rows - 1) — for rows = 4, last is 'D'.

3. Same countdown core. Only the source of last 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 — Reverse substring

Build A..peak with substring, reverse it, and print — same shape, no explicit inner countdown.

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

        for (int i = 1; i <= rows; i++) {
            StringBuilder sb = new StringBuilder(letters.substring(0, i));
            System.out.println(sb.reverse());
        }
    }
}

How It Works

1. Slice the forward prefix. substring(0, i) is A, AB, ABC, … — same as Program 1.

2. Reverse in place. StringBuilder.reverse() turns that prefix into A, BA, CBA, …

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.

j++

Forward triangle by mistake

If the inner loop increments from A to i, you reprint Program 1. Keep j-- from i down to 'A'.

Stop at B

Missing final A

Stopping before 'A' drops the tip letter every row. The condition must be j >= '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 — 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)
Reverse substring (Example 3)O(rows²)O(rows) per temporary StringBuilder

Total letters = 1 + 2 + … + n = n(n + 1)/2 — still quadratic in n. Same totals as Program 1; only print order along each row differs.

Key Takeaways

  • Rule: peak letter grows A..last; print that peak down to A.
  • Flip of Program 1: same widths — reverse the inner loop direction.
  • 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 each peak i from 'A' to the last letter, print i down to 'A', then println.

Frequently Asked Questions

The outer loop selects the highest letter on the row (A to E). The inner loop starts at that letter and decrements down to A, printing each character, so each row reads backward.
Because the inner loop always runs until j equals A, so the last printed character on every row is A.
Program 1 prints forward along each row (A, AB, ABC). This pattern prints backward down to A (A, BA, CBA) while the left edge still grows A, B, C, …
Program 3 starts earlier each row but still prints forward to a fixed top (E, DE, CDE). This pattern starts later each row and prints backward to a fixed A.
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 the total printed letters are 1+2+…+n = n(n+1)/2.
Yes. Take letters.substring(0, i), reverse it with StringBuilder, and println the result. Nested char loops are better for learning; reverse 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?

Each row starts one letter later (A, B, C, …), but prints backward down to A. For 5 rows, the output is A, BA, CBA, DCBA, EDCBA — the reverse of Program 1’s row order.

Next: Inverted Alphabet Triangle

Shrink the end letter each row while still printing forward from A.

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