V-Shaped Alphabet Pattern in Java

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\)):
A A
B B
C C
D D
EJava Program (Reference Logic)
Two scans per row: one left-to-right, one right-to-left (starting from D).
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
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).
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.
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.
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.
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.
Variation — User Input
Generalize to any rows (A..endChar). The right scan starts at endChar - 1.
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
rowswithin 26 if you want only letters A..Z
Avoid
- Including
endCharin the right scan unless you want a double vertex - Using a proportional font when you care about alignment
Key Takeaways
Only diagonal positions print letters; everything else is spaces.
Right scan is length n - 1 so width is 2n - 1.
Bottom row prints a single vertex letter.
O(n²) time for n rows.
❓ Frequently Asked Questions
E twice (left and right), changing the intended V-shape.More Java alphabet patterns
Diagonal patterns are often just equality checks between row and column indices.
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.
12 people found this page helpful
