Java Inverted V Alphabet Pattern

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

What Is This Pattern?

An inverted V-shaped alphabet pattern prints a single tip letter at the top, then matching letter pairs that drift farther apart on each lower row.

Remember
Rule: left print when i == j; right print when i == k
      (right scan starts at B so tip A stays alone)

    A    
   B B   
  C   C  
 D     D 
E       E     ← A–E (width 9)

Geometry matches the hollow inverted V in Star Pattern 7, but cells print letters instead of *. Stack a mirrored lower half in Program 34 to close a full alphabet diamond.

How to Solve It

Two ways to emit the same outline — start with if/else legs, then optionally share a cell helper.

MethodIdeaBest for
If/else legsLeft j and right k scans; letter when indices matchLearning, interviews, exams
Helper + ternaryOne printCell(row, col) used by both legsLess duplication once the diagonals click

Pseudocode

Pseudocode
n = endLetter - 'A'          // 4 when end is 'E'
for i from 0 to n:
    for j from n down to 0:
        print alpha[j] if i == j else " "
    for k from 1 to n:
        print alpha[k] if i == k else " "
    print newline

Cheat sheet

GoalPattern
Scale from end letterint n = end - 'A';
Walk each rowfor (int i = 0; i <= n; i++)
Left diagonalfor (int j = n; j >= 0; j--) + if (i == j)
Right diagonalfor (int k = 1; k <= n; k++) + if (i == k)
Line width2 * n + 1
End the rowSystem.out.println();
Full diamond nextProgram 34

Printing Letters vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach letter and each space
System.out.printlnEnds the current lineAfter both inner loops

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

Live Preview

Change the end letter and the inverted V updates instantly — including width and letter count.

One letter from A to Z. Width is 2 * (end - 'A') + 1.

Live result A–E · 5 rows · 9 letters
    A    
   B B   
  C   C  
 D     D 
E       E

Worked Walkthrough — A–D (n = 3)

Trace where each letter lands for every outer-loop value of i (line width = 7).

iLeft (j)Right (k)LettersPrinted row
0 (A)j == 0 → Anone (k starts at 1)1A
1 (B)j == 1 → Bk == 1 → B2B B
2 (C)j == 2 → Ck == 2 → C2C C
3 (D)j == 3 → Dk == 3 → D2D D

Row 0 is the only single-letter line — that is why the right loop must not start at k = 0. Total letters: 1 + 2 + 2 + 2 = 7 = 2×3 + 1.

Java Programs

Three complete programs: fixed A–E, end-letter input, and a reusable cell helper. Use View Output to reveal sample results.

Example 1 — Fixed A–E

Hard-coded range — left scan j = 4..0, right scan k = 1..4, letter when indices match.

Java
public class InvertedVAlphabet {
    public static void main(String[] args) {
        char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

        for (int i = 0; i <= 4; i++) {
            for (int j = 4; j >= 0; j--) {
                if (i == j)
                    System.out.print(alpha[j]);
                else
                    System.out.print(" ");
            }
            for (int k = 1; k <= 4; k++) {
                if (i == k)
                    System.out.print(alpha[k]);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

How It Works

1. Alphabet table. alpha[0] is A, alpha[4] is E.

2. Outer loop picks the row. i runs from 0 (tip A) to 4 (widest E pair).

3. Left diagonal. j counts from 4 down to 0; print alpha[j] only when i == j.

4. Right diagonal, then break. k runs from 1 to 4 with the same match rule, then println.

When i = 0 only the left loop prints; when i = 4 both outer columns print E.

Example 2 — End Letter Input

Read the end letter and scale both scans with n = end - 'A'. Prefer validating a single A–Z character in real apps.

Java
import java.util.Scanner;

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

        System.out.print("Enter end letter (like E): ");
        char end = sc.next().toUpperCase().charAt(0);

        int n = end - 'A';
        char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

        for (int i = 0; i <= n; i++) {
            for (int j = n; j >= 0; j--)
                System.out.print(i == j ? alpha[j] : ' ');
            for (int k = 1; k <= n; k++)
                System.out.print(i == k ? alpha[k] : ' ');
            System.out.println();
        }

        sc.close();
    }
}

How It Works

1. Prompt and normalize. Read a token, uppercase it, and take the first character.

2. Scale the scans. For end = C, n = 2 — width 5, tip still a single A.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNext()) {
    System.out.println("Enter a single letter A–Z.");
    return;
}
String raw = sc.next().trim().toUpperCase();
if (raw.length() != 1 || raw.charAt(0) < 'A' || raw.charAt(0) > 'Z') {
    System.out.println("Enter a single letter A–Z.");
    return;
}
char end = raw.charAt(0);

Example 3 — Helper Method

Extract one cell printer so both diagonal loops stay thin.

Java
public class InvertedVAlphabetHelper {
    static void printCell(char[] alpha, int row, int col) {
        System.out.print(row == col ? alpha[col] : ' ');
    }

    public static void main(String[] args) {
        int n = 4;
        char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

        for (int i = 0; i <= n; i++) {
            for (int j = n; j >= 0; j--)
                printCell(alpha, i, j);
            for (int k = 1; k <= n; k++)
                printCell(alpha, i, k);
            System.out.println();
        }
    }
}

How It Works

1. One cell rule. printCell owns the row == col decision and the space fallback.

2. Same bounds. Left still counts down from n; right still starts at 1.

3. Same shape, less copy-paste. Learn the expanded if/else first (Example 1), then refactor when the diagonals feel familiar.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = 0

Duplicate tip A

Starting the right loop at k = 0 prints two As on the first row. Keep k = 1.

j ascending

Mirrored left leg

The left loop must count j from n down to 0. Ascending j flips the left diagonal.

println inside

Broken outline

If println is inside either inner loop, each cell lands on its own line. Use print for cells; println only after both loops.

end = A

Single tip

Output is just A — right loop never runs. A good sanity check.

Proportional font

Looks skewed in the IDE

Spaces and letters need a monospace font. Proportional fonts make diagonals look uneven.

Bad input

Validate one letter

Empty tokens and multi-character input break naive charAt(0) — require a single A–Z character.

Time and Space Complexity

ProgramTimeExtra space
If/else legs (Examples 1–2)O(n²)O(1) beyond the alphabet array
Helper method (Example 3)O(n²)O(1) beyond the alphabet array

About n + 1 rows × 2n + 1 characters printed per row — still quadratic in n. Total letters = 2n + 1 (one tip + two per later row).

Key Takeaways

  • Rule: print alpha[col] only when row == col; otherwise a space.
  • Two legs: left j counts down from n; right k starts at 1.
  • Break the row: call println only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space beyond the alphabet table.

One line: for each row i, print a letter only when the left or right column index matches i — start the right loop at 1.

Frequently Asked Questions

Because the right block starts from index 1 (letter B), so it never matches i equals 0. Only the left block prints A on the first row.
Width is 2n+1: n+1 columns from the left block and n columns from the right block. For A–E, n is 4 and width is 9.
Program 31 is wide at the top and has a single bottom vertex. Program 33 has a single A at the top and widens downward with pairs like B B, C C.
System.out.print stays on the same line. System.out.println ends the current line. Letters and spaces use print; the row break uses println after both inner loops.
O(n²) because there are n+1 rows and each row scans O(n) positions across both blocks.
Spaces keep column alignment so the two diagonals open into a visible inverted V in a monospace console.
Use Scanner next(), take charAt(0) after toUpperCase(), require A–Z, and reject empty or multi-character tokens.
Program 34 reuses this inverted-V row logic for A..E, then mirrors D..A downward to close a full diamond without repeating the widest E row.

Did you know?

This inverted V is the upper half of the alphabet diamond. Starting the right loop at k = 1 (letter B) is deliberate: on row 0 the left loop already prints the tip A, so visiting index 0 again would duplicate it.

Next: Alphabet Diamond

Stack this inverted V with a mirrored lower half to close a full diamond.

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