Inverted V-Shaped Alphabet Pattern in Java

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

What You’ll Learn

This pattern draws an inverted V: one A at the top, then pairs like B B, C C, and so on, widening as you go down.

We scan a fixed-width row and print letters only where the row letter matches the current column index.

⭐ Pattern Output

Output for 5 rows:

Output
    A
   B B
  C   C
 D     D
E       E
1

Java Program (Reference Logic)

Left scan from E to A, then right scan from B to E.

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

🧠 How It Works

1

Row letter i

for (i = 65; i <= 69; i++) walks each row (ASCII AE). The current letter is the one that may appear on both legs (except the apex row, which only prints once).

Row
2

Left block j (E..A)

for (j = 69; j >= 65; j--) prints (char) j only when i == j; otherwise a space. That draws the descending diagonal.

Left
3

Right block k (B..E)

for (k = 66; k <= 69; k++) mirrors the rule on the ascending side. Starting at 66 (B) skips a duplicate A on the first row while still forming the widening shape.

Right
4

Symmetry & newline

Both inner loops use the same predicate (i == column) so legs stay aligned. System.out.println() after both blocks ends the row.

Align
=

Opening downward

Each row adds width between the legs. Five rows with two O(n) passes per row → O(n²) time and O(1) extra space beyond the stream.

2

Variation — User Input

For rows, set endChar = 'A' + rows - 1 and run the same two scans; the right scan starts at startChar + 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 = endChar; j >= startChar; j--) {
                if (i == j) System.out.print(j);
                else System.out.print(' ');
            }
            for (char k = (char) (startChar + 1); k <= endChar; k++) {
                if (i == k) System.out.print(k);
                else System.out.print(' ');
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Stretch the shape by printing two spaces instead of one
  • Fill the inside region for a solid triangle
  • Keep rows <= 26 for A..Z only

Avoid

  • Starting the right scan at A if you want a single apex
  • Proportional fonts when evaluating alignment

Key Takeaways

1

Two diagonal checks pick out the two legs.

2

Right block starts at B so the first row prints only one A.

3

Width is 2n - 1 for n letters.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

So row A doesn’t print twice. From row B onward, both legs can match the same letter.
O(n²) because each row scans O(n) columns and there are n rows.

More Java alphabet patterns

Once you can pick diagonal positions with equality checks, you can draw many shapes in a console grid.

All Alphabet Patterns →
Did you know?

If the right block started at A, the top row would print two As (one on each side) unless you changed the ranges.

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