Print Alphabet Diamond in Java
intermediate· Patterns · Character Patterns
Problem
A diamond is just a pyramid immediately followed by that same pyramid's upside-down mirror, with the shared widest row printed only once at the seam.
Given a number of rows for its top half, print a diamond shape where each row is filled with a single letter of the alphabet.
Input
5
Output
A
B B B
C C C C C
D D D D D D D
E E E E E E E E E
D D D D D D D
C C C C C
B B B
A
Java Program
Java
public class AlphabetDiamond {
static void printRow(int i, int n) {
char letter = (char) ('A' + i - 1);
StringBuilder row = new StringBuilder();
for (int s = 0; s < n - i; s++) row.append(" ");
for (int j = 0; j < 2 * i - 1; j++) {
if (j > 0) row.append(" ");
row.append(letter);
}
System.out.println(row);
}
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) printRow(i, n);
for (int i = n - 1; i >= 1; i--) printRow(i, n); // skip n itself, already printed above
}
}Output
A
B B B
C C C C C
D D D D D D D
E E E E E E E E E
D D D D D D D
C C C C C
B B B
A
Core Logic
Pulling the per-row letter-and-spacing logic out into its own method means the diamond's top half and bottom half can both call it, just counting in opposite directions.
How It Works
- 1
printRow(i, n)builds one row exactly the way Alphabet Pyramid does —n - ileading spaces, then letter'A' + i - 1repeated2 * i - 1times. - 2The top half calls
printRow(i, n)forifrom1ton, growing the pyramid up to its widest row. - 3The bottom half calls it again for
ifromn - 1down to1, mirroring the top half without repeating the widest row. - 4Because both halves share one method, the diamond's two sides can never drift out of sync with each other.
For
n = 5, the top half prints rows for letters A through E; the bottom half then prints D down through A, mirroring everything except the shared E row.💡
Key Point: The bottom half starts at n - 1, not n — repeating the widest row would print it twice at the seam where the two halves meet.
Complexity
Time Complexity: O(n²)Space Complexity: O(1)
Why: Both halves together print a number of characters proportional to n², the same growth rate as a single pyramid, just doubled.
Key Concepts
nested for loophelper methodchar arithmetic