Java V-Shaped Alphabet Pattern

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

What Is This Pattern?

A V-shaped alphabet pattern prints letters only on two diagonals that meet at a single tip, with spaces everywhere else so the shape reads as a V in a monospace console.

Remember
Rule: left 0..n when i==j; right n−1..0 when i==k

A       A
 B     B
  C   C
   D D
    E         ← tip once (end = 'E')

The right scan starts at n − 1 (not n) so the tip letter prints once. Compare with Program 33 (inverted V) and Program 32 (solid palindrome pyramid).

How to Solve It

Two ways to emit the same V — inline diagonal checks, or a shared printCell helper.

MethodIdeaBest for
Two scans inlineLeft 0..n, right n−1..0, print when row == colLearning, interviews, exams
printCell helperOne method owns the diagonal rule for both legsCleaner demos once the tip skip clicks

Pseudocode

Pseudocode
n = end - 'A'
for i from 0 to n:
    for j from 0 to n:
        print (i == j ? alpha[j] : ' ')
    for k from n - 1 down to 0:
        print (i == k ? alpha[k] : ' ')
    print newline

Cheat sheet

GoalPattern
End indexint n = end - 'A'; (4 for E)
Rowsfor (int i = 0; i <= n; i++)
Left legfor (int j = 0; j <= n; j++) print(i == j ? alpha[j] : ' ');
Right legfor (int k = n - 1; k >= 0; k--) print(i == k ? alpha[k] : ' ');
Single tipRight scan starts at n − 1, not n
Width(n + 1) + n = 2n + 1
Spaces matterNon-hits print ' ' to keep columns aligned

Printing Letters vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach letter or space cell
System.out.printlnEnds the current lineAfter both scans finish a row

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

Live Preview

Change the end letter and the V updates instantly — width is always 2n + 1, with a single tip on the last row.

One letter from A to F. Tap a chip or type a letter — use a monospace view for alignment.

Live result End E · 5 rows · width 9
A       A
 B     B
  C   C
   D D
    E

Worked Walkthrough — End = E (n = 4)

Trace each row’s diagonal hits and the resulting 9-column line.

iLeft hitRight hitPrinted row
0A at j=0A at k=0A A
1B at j=1B at k=1B B
2C at j=2C at k=2C C
3D at j=3D at k=3D D
4E at j=4(none)E

Width is always 2×4 + 1 = 9. The tip row has no right-leg match because k never equals 4.

Java Programs

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

Example 1 — Fixed A–E

Two scans per row: left-to-right (A..E) and right-to-left (D..A). Letters print only when the row matches the column.

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

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

            for (int k = 3; k >= 0; k--) {
                if (i == k) System.out.print(alpha[k]);
                else System.out.print(" ");
            }

            System.out.println();
        }
    }
}

How It Works

1. Outer loop picks the row. i runs from 0 to 4 (letters A…E).

2. Left leg — main diagonal. Scan j = 0..4; print the letter only when i == j, else a space.

3. Right leg — mirrored diagonal. Scan k = 3..0; print when i == k. Starting at 3 skips a second tip.

4. Tip row. When i = 4, only the left scan can print E.

Example 2 — End Letter Input

The right scan starts at end − 1 so the vertex prints once. Prefer validating a single A–Z character in real apps.

Java
import java.util.Scanner;

public class VShapedAlphabetInput {
    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 = 0; j <= n; j++)
                System.out.print(i == j ? alpha[j] : ' ');

            for (int k = n - 1; k >= 0; k--)
                System.out.print(i == k ? alpha[k] : ' ');

            System.out.println();
        }

        sc.close();
    }
}

How It Works

1. Scale with n. n = end - 'A' drives both scans. For end = C, width is 2×2 + 1 = 5.

2. Same tip rule. Right loop still starts at n − 1 so the tip is a single letter.

3. Safer input tip. Prefer:

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

Example 3 — Helper Method

Often clearer: one method applies the diagonal rule so left and right loops stay thin.

Java
public class VShapedAlphabetHelper {
    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 = 0; j <= n; j++)
                printCell(alpha, i, j);

            for (int k = n - 1; k >= 0; k--)
                printCell(alpha, i, k);

            System.out.println();
        }
    }
}

How It Works

1. printCell owns the rule. row == col lives in one place for both legs.

2. Tip still skipped on the right. The right loop starts at n − 1 even with the helper.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = n

Double tip

If the right scan includes index n, the last row prints the tip twice. Keep for (k = n - 1; k >= 0; k--).

No spaces

Collapsed V

Writing only letters (no spaces) packs both legs together. Non-hits must print ' '.

println inside

Column of cells

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

end = A

Single A

When n = 0, left prints A and the right loop never runs. A good sanity check.

Wrong compare

Off-diagonal letters

Always compare the row index to the current column (i == j / i == k), not to a fixed letter.

Bad input

Validate one letter

Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.

Time and Space Complexity

ProgramTimeExtra space
Two scans inline (Examples 1–2)O(n²)O(1) (plus the fixed alphabet table)
Helper method (Example 3)O(n²)O(1)

For letters A..end there are n + 1 rows and each row scans 2n + 1 cells — quadratic in n.

Key Takeaways

  • Rule: print a letter only when row index equals column index.
  • Single tip: right scan starts at n − 1, not n.
  • Spaces keep the V: non-hits print ' '; break the row with println after both scans.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row, scan left 0..n and right n−1..0, printing a letter only on the diagonal.

Frequently Asked Questions

If the right scan included E, the last row would print E twice (one on each side), breaking the single vertex at the bottom tip of the V.
For n = end − 'A', the left block is n+1 columns and the right block is n columns, so total width is 2n+1 (9 for A..E).
Usually two (one per leg), except the bottom row prints one letter because the right loop cannot match the tip index.
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 form a visible V in a monospace console.
System.out.print stays on the same line. System.out.println ends the current line. Cells use print; the row break uses println after both scans.
Yes. Set n = end − 'A', scan j from 0 to n on the left, and scan k from n−1 down to 0 on the right.
Program 20 also practices diagonal alignment with letters; this page focuses on a V made from two opposing diagonal scans that meet at one vertex.

Did you know?

Outer i is the row index (A..E). Left scan prints only when i == j (main diagonal). Right scan runs from D down to A so the bottom vertex letter (E) appears once.

Next: Symmetric Alphabet Pyramid

Move from a hollow V to centered palindrome rows like A, ABA, ABCBA, and ABCDEDCBA.

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