Inverted V-Shaped Alphabet Pattern in Java

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:
A
B B
C C
D D
E EJava Program (Reference Logic)
Left scan from E to A, then right scan from B to E.
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
Row letter i
for (i = 65; i <= 69; i++) walks each row (ASCII A…E). The current letter is the one that may appear on both legs (except the apex row, which only prints once).
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.
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.
Symmetry & newline
Both inner loops use the same predicate (i == column) so legs stay aligned. System.out.println() after both blocks ends the row.
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.
Variation — User Input
For rows, set endChar = 'A' + rows - 1 and run the same two scans; the right scan starts at startChar + 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 = 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 <= 26for A..Z only
Avoid
- Starting the right scan at
Aif you want a single apex - Proportional fonts when evaluating alignment
Key Takeaways
Two diagonal checks pick out the two legs.
Right block starts at B so the first row prints only one A.
Width is 2n - 1 for n letters.
O(n²) time for n rows.
❓ Frequently Asked Questions
A doesn’t print twice. From row B onward, both legs can match the same letter.More Java alphabet patterns
Once you can pick diagonal positions with equality checks, you can draw many shapes in a console grid.
If the right block started at A, the top row would print two As (one on each side) unless you changed the ranges.
12 people found this page helpful
