Java Program to Print Diamond Outline Character Pattern
In this program we print the outline of a diamond with letters. The diamond grows from A to the chosen letter and shrinks back to A, and each row prints its letter twice, on the left and right edges.
Like every pattern program, it is built from nested loops: the outer loop picks the row, and the inner loops print what goes on that row. Java stores a char as a number, so adding to 'A' and casting back with (char) gives the following letters.
1. The Pattern
For the letter E the program prints:
Pattern
A
B B
C C
D D
E E
D D
C C
B B
A
2. How the Program Works
sizeis how farlastLetteris from A: 4 for E. The widest row is rowsize.- For each row, the line starts with
size - rowspaces and the row letter. Every row except the first then gets2 * row - 1spaces and the letter again, which makes the right edge. - Step 1 prints each line of the top half and also stores it in the
linesarray. - Step 2 prints the stored lines again from
size - 1down to 0, which gives the bottom half without repeating the widest row. String.repeat()needs Java 11 or later.
3. Java Program
public class Pattern19 {
public static void main(String[] args) {
char lastLetter = 'E';
int size = lastLetter - 'A';
String[] lines = new String[size + 1];
// Step 1: build and print the top half, from A down to lastLetter
for (int row = 0; row <= size; row++) {
char letter = (char) ('A' + row);
String line = " ".repeat(size - row) + letter;
if (row > 0) {
line += " ".repeat(2 * row - 1) + letter;
}
lines[row] = line;
System.out.println(line);
}
// Step 2: print the top half again in reverse, without repeating the middle row
for (int row = size - 1; row >= 0; row--) {
System.out.println(lines[row]);
}
}
}
Output
A
B B
C C
D D
E E
D D
C C
B B
A
4. Print the Diamond for Any Letter
To let the user choose how big the diamond is, read the last letter with a Scanner. The loops stay the same.
import java.util.Scanner;
public class Pattern19Input {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a letter from A to Z: ");
char lastLetter = Character.toUpperCase(scanner.next().charAt(0));
scanner.close();
int size = lastLetter - 'A';
String[] lines = new String[size + 1];
// Step 1: build and print the top half, from A down to lastLetter
for (int row = 0; row <= size; row++) {
char letter = (char) ('A' + row);
String line = " ".repeat(size - row) + letter;
if (row > 0) {
line += " ".repeat(2 * row - 1) + letter;
}
lines[row] = line;
System.out.println(line);
}
// Step 2: print the top half again in reverse, without repeating the middle row
for (int row = size - 1; row >= 0; row--) {
System.out.println(lines[row]);
}
}
}
Output (with sample input)
Enter a letter from A to Z: G
A
B B
C C
D D
E E
F F
G G
F F
E E
D D
C C
B B
A
Note: the program expects a capital or small letter from A to Z. Anything else, such as a digit, gives a negative
size and the program fails.5. Summary
- The outer loop picks the row; the inner loops print the spaces and the letters for that row.
(char) ('A' + k)gives the letterkplaces after A.- Keep the last letter in a variable, so the same program can print the pattern in any size.
Related Programs
Was this tutorial helpful?