Diamond-Shaped Alphabet Pattern in Java

What You’ll Learn
This pattern is a full diamond made by stacking the inverted V from program 33 and then mirroring it back upward.
The only extra trick: start the bottom half from D so the widest row (E) is printed only once.
⭐ Pattern Output
Output for A.. E:
A
B B
C C
D D
E E
D D
C C
B B
AJava Program (Reference Logic)
Two phases with identical inner loops; second phase starts at D to avoid duplicating the E row.
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();
}
for (i = 68; i >= 65; 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
Top outer loop (grow the V)
i runs 65…69 (A…E). Each larger i places the letter farther out on both diagonals so the hollow shape opens toward the widest E row.
Two inner passes per row
First, j scans E down to A; only when i == j does System.out.print((char) j) run, else a space—this draws the left leg. Then k scans B through E with the same equality rule—this draws the right leg.
Bottom outer loop (shrink the V)
The second part runs i from 68 down to 65 (D…A) with the same inner loops pasted again, rebuilding the arms in reverse so the diamond closes symmetrically.
Why the bottom starts at D
The top half already printed the single-wide E row. Starting i at 68 skips repeating that middle line while still mirroring every narrower row.
Nine rows, two scans each
Five growing rows plus four shrinking rows; every row runs two full passes over the letter ranges. That is O(n²) character decisions for n letters along each leg.
Variation — User Input
Enter rows (half height). Bottom half starts at endChar - 1 to avoid duplicating the center row.
import java.util.Scanner;
public class Main {
static void printRow(char i, char startChar, char endChar) {
for (char j = endChar; j >= startChar; j--)
System.out.print(i == j ? j : ' ');
for (char k = (char) (startChar + 1); k <= endChar; k++)
System.out.print(i == k ? k : ' ');
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows (half): ");
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++)
printRow(i, startChar, endChar);
for (char i = (char) (endChar - 1); i >= startChar; i--)
printRow(i, startChar, endChar);
sc.close();
}
}💡 Tips for Enhancement
Try These
- Fill the interior between the legs for a solid diamond
- Use two spaces per blank to stretch the diamond
- Keep
rowswithin 26 for A..Z
Avoid
- Starting the bottom half at
endChar(duplicates the center row) - Proportional fonts when you want perfect alignment
Key Takeaways
Diamond = top half + mirrored bottom half.
Start bottom half at endChar - 1 to avoid a duplicate center.
Rows and width both scale as \(2n-1\).
O(n²) time for n letters.
❓ Frequently Asked Questions
endChar - 1, skipping the already-printed endChar row.More Java pattern tutorials
Once you can mirror a shape vertically, you can create diamonds, hourglasses, and many other symmetric patterns.
The same row-printing logic is reused for both halves—only the range of the outer loop changes to mirror the output.
12 people found this page helpful
