V-Shaped Alphabet Pattern in Java

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

This pattern prints letters only on the two diagonals that form a V. Everywhere else, it prints spaces.

The last row prints a single vertex letter (E), because the right diagonal intentionally skips the last letter.

⭐ Pattern Output

Output for 5 letters (width \(2n-1 = 9\)):

Output
A       A
 B     B
  C   C
   D D
    E
1

Java Program (Reference Logic)

Two scans per row: one left-to-right, one right-to-left (starting from D).

Java
public class Main {
    public static void main(String[] args) {
        int i, j, k;
        for (i = 65; i <= 69; i++) {
            for (j = 65; j <= 69; j++) {
                if (i == j) System.out.print((char) j);
                else System.out.print(" ");
            }
            for (k = 68; k >= 65; k--) {
                if (i == k) System.out.print((char) k);
                else System.out.print(" ");
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Outer i (65–69)

for (i = 65; i <= 69; i++) walks each row; i is the ASCII code of the letter that should appear on the two legs for that row (and only once at the apex).

Row
2

Left scan j

for (j = 65; j <= 69; j++): if i == j, print (char) j; else a space. Exactly one column matches per row, forming the downward leg from the left.

Diag
3

Right scan k from 68 down

for (k = 68; k >= 65; k--) uses the same i == k rule. Starting at 68 ('D') skips a second 'E' on the bottom row while still drawing the inward leg.

Mirror
4

Newline & width

After both passes, System.out.println() ends the row. Each line has a fixed character count (five left slots plus four right), so alignment depends on a monospace font in the terminal.

Width
=

Converging V

As i increases, the matching columns move toward the center. Five rows × two O(n) inner loops → O(n²) time, O(1) extra space.

2

Variation — User Input

Generalize to any rows (A..endChar). The right scan starts at endChar - 1.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();

        rows = Math.max(1, Math.min(rows, 26));
        char startChar = 'A';
        char endChar = (char) ('A' + rows - 1);

        for (char i = startChar; i <= endChar; i++) {
            for (char j = startChar; j <= endChar; j++)
                System.out.print(i == j ? j : ' ');
            for (char k = (char) (endChar - 1); k >= startChar; k--)
                System.out.print(i == k ? k : ' ');
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Double the horizontal gap by printing two spaces instead of one
  • Star version: print * on diagonals using the same equality checks
  • Keep rows within 26 if you want only letters A..Z

Avoid

  • Including endChar in the right scan unless you want a double vertex
  • Using a proportional font when you care about alignment

Key Takeaways

1

Only diagonal positions print letters; everything else is spaces.

2

Right scan is length n - 1 so width is 2n - 1.

3

Bottom row prints a single vertex letter.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

It can, but then the bottom row would print E twice (left and right), changing the intended V-shape.
O(n²) because each row scans across \(2n-1\) positions.

More Java alphabet patterns

Diagonal patterns are often just equality checks between row and column indices.

All Alphabet Patterns →
Did you know?

On each row, at most two cells print letters (one per leg)—except the last row prints only one because the right scan does not include the last letter.

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