Diamond Alphabet & Stars in Java

What You’ll Learn
Build a vertical diamond where each horizontal line is the same letter separated by *, widening to the middle then narrowing again.
Compare Java star patterns (geometry only) and program 15 (mirror with a star block).
⭐ Pattern Output
Half height 5:
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
AComplete Java Program (Half Height 5)
Odd j prints the row letter, even j prints *. Upper half prints 1..5, lower half prints 4..1.
public class Main {
public static void main(String[] args) {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j < i * 2; j++) {
if (j % 2 == 0) {
System.out.print("*");
} else {
System.out.print((char) (i + 64));
}
}
System.out.println();
}
for (i = 4; i >= 1; i--) {
for (j = 1; j < i * 2; j++) {
if (j % 2 == 0) {
System.out.print("*");
} else {
System.out.print((char) (i + 64));
}
}
System.out.println();
}
}
}🧠 How It Works
Upper half: i = 1 .. n
Each row prints one repeated letter using (char)(i + 64) or equivalently (char)('A' + i - 1). Row 1 is only A; wider rows alternate that letter with *.
Inner stripe: j from 1 to 2i - 1
for (j = 1; j < i * 2; j++) visits an odd number of positions. Even j prints *; odd j prints the row letter. That produces B*B, C*C*C, … with stars between letters.
Lower half: i = n-1 .. 1
After the peak row at i = n, a second outer loop counts i down with the same inner body so the diamond narrows again without printing the widest line twice.
Why width is 2i - 1
Row index i needs i letters and i - 1 star gaps, hence 2i - 1 symbols. For n = 5 the middle line has nine tokens.
Diamond cost
Upper and lower halves each sum odd widths 1 + 3 + … + (2n-1) with the shared middle counted once in this split-loop style. Overall work is Θ(n²) for half-height n.
Variation — User Input (Half Height)
Lower loop starts at rows - 1 so the widest row prints once. Clamp rows to 1–26.
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 (half): ");
int rows = sc.nextInt();
rows = Math.max(1, Math.min(rows, 26));
int i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j < i * 2; j++) {
if (j % 2 == 0) {
System.out.print("*");
} else {
System.out.print((char) ('A' + i - 1));
}
}
System.out.println();
}
for (i = rows - 1; i >= 1; i--) {
for (j = 1; j < i * 2; j++) {
if (j % 2 == 0) {
System.out.print("*");
} else {
System.out.print((char) ('A' + i - 1));
}
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Add leading spaces so the diamond is centered in the terminal
- Swap
*for-or another separator - Cap
rowsso'A' + rows - 1 <= 'Z'
Avoid
- Starting the lower loop at
rows(duplicates the widest row) - Flipping the parity branches without also changing the intended pattern
Key Takeaways
Odd j prints the letter, even j prints *.
j < 2*i gives 2i - 1 symbols per row.
Mirror with i = n-1 .. 1 after 1 .. n.
O(n²) prints for half-height n.
❓ Frequently Asked Questions
j prints *; odd j prints the row letter.A); the lower loop starts at 0 and does nothing.'A' + i - 1 states the intent more clearly.More Java alphabet patterns
Two-phase loops (up then down) are the standard way to code diamonds without repeating the center line.
On row i the letter appears i times and * appears i - 1 times, for a total of 2i - 1 characters.
12 people found this page helpful
