Published: 29 Apr 2025 | Reading Time: 6 min read
Creating a diamond pattern in Java is a great way to practice working with loops, conditional statements, and logical thinking. A diamond pattern includes two symmetrical triangles that meet at their widest point, forming a diamond shape. Generally, these patterns are displayed using asterisks (*), but you can also use numbers, letters, or other symbols.
In this guide, we'll go through different diamond patterns, including the basic solid diamond and variations like the hollow diamond. We'll also cover how to implement them using different loops, such as for, while, and do-while, so you can understand the various ways to approach pattern printing in Java.
The diamond pattern in Java is a commonly used pattern that forms a symmetrical shape resembling a diamond using stars (*). It consists of two parts: the top half and the bottom half. The top half starts with one star in the first row, gradually increasing until the center, while the bottom half decreases in a reverse order, forming the lower part of the diamond.
To generate this pattern programmatically, nested loops are commonly used. The outer loop controls the rows, while the inner loops handle printing the required spaces and stars for each row. The number of spaces before the stars decreases as the row number increases in the top half, and it increases again as the row number decreases in the bottom half.
The diamond pattern in Java is more than just visually attractive. It helps programmers develop essential coding skills. Here's why they're important:
1. Input: Read the number of rows (n) for the top half of the diamond (typically an odd number).
2. Top Half of the Diamond:
For each row i from 1 to n:
3. Bottom Half of the Diamond:
For each row i from n-1 down to 1:
4. Output: The result is a symmetrical diamond pattern printed on the console.
Input: number of rows n
For i = 1 to n:
Print (n - i) spaces
Print (2 * i - 1) stars
For i = n-1 down to 1:
Print (n - i) spaces
Print (2 * i - 1) stars
A diamond pattern in Java is a symmetrical shape made up of stars (*), where the number of stars increases up to a midpoint and then decreases symmetrically. This program uses nested for loops to control spaces and stars. The logic is divided into two parts: the upper half (expanding) and the lower half (contracting) to create the diamond shape. Here is the Java program to achieve this:
public class DiamondPattern {
public static void main(String[] args) {
int n = 5; // Number of rows for the upper half
// Printing the upper half of the diamond
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
// Printing the lower half of the diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
}
}
This program prints a diamond pattern by first creating an increasing triangle (upper half) and then reflecting it as a decreasing triangle (lower half). The first inner loop adds top spaces to align the stars properly, while the second inner loop prints the required number of stars. The number of stars follows the formula (2 * i - 1) for each row.
*
***
*****
*******
*********
*******
*****
***
*
Just like with the for loop, we can also use a while loop to print a symmetrical diamond pattern in Java. The approach remains the same. The program first prints an increasing triangle (upper half), followed by a decreasing triangle (lower half). The while loop ensures that the number of spaces and stars is correctly aligned in each row. Here is the Java program implementing this logic using a while loop.
public class DiamondPatternWhile {
public static void main(String[] args) {
int n = 5, i = 1;
// Printing the upper half of the diamond
while (i <= n) {
int j = i;
while (j < n) {
System.out.print(" ");
j++;
}
int k = 1;
while (k <= (2 * i - 1)) {
System.out.print("*");
k++;
}
System.out.println();
i++;
}
// Printing the lower half of the diamond
i = n - 1;
while (i >= 1) {
int j = n;
while (j > i) {
System.out.print(" ");
j--;
}
int k = 1;
while (k <= (2 * i - 1)) {
System.out.print("*");
k++;
}
System.out.println();
i--;
}
}
}
This program follows the same pattern logic as the for loop version but uses while loops instead. The first while loop constructs the upper half of the diamond by printing leading spaces and stars in increasing order. After reaching the widest row, the second while loop mirrors the pattern by decreasing the number of stars per row. The number of stars in each row follows the formula (2 * i - 1), for symmetry.
*
***
*****
*******
*********
*******
*****
***
*
A do-while loop can also be used to print a diamond pattern in Java. Unlike for and while loops, the do-while loop ensures that the block of code executes at least once before checking the condition. This program consists of two parts: the upper half (expanding pattern) and the lower half (contracting pattern). Each iteration properly aligns spaces and stars to maintain the diamond shape. Here is the Java program implementing this logic using a do-while loop.
public class DiamondPatternDoWhile {
public static void main(String[] args) {
int n = 5, i = 1;
// Printing the upper half of the diamond
do {
int j = i;
do {
System.out.print(" ");
j++;
} while (j <= n);
int k = 1;
do {
System.out.print("*");
k++;
} while (k <= (2 * i - 1));
System.out.println();
i++;
} while (i <= n);
// Printing the lower half of the diamond
i = n - 1;
do {
int j = n;
do {
System.out.print(" ");
j--;
} while (j > i);
int k = 1;
do {
System.out.print("*");
k++;
} while (k <= (2 * i - 1));
System.out.println();
i--;
} while (i >= 1);
}
}
This program prints a diamond pattern using do-while loops. The upper half of the diamond is created by printing spaces first, followed by stars. The lower half mirrors this pattern in reverse order. The do-while loop ensures that the pattern is printed at least once, even if the initial condition is false. The number of stars per row follows the formula (2 * i - 1) for symmetry.
*
***
*****
*******
*********
*******
*****
***
*
A hollow diamond pattern is a variation of the standard diamond pattern where only the edges are printed using *, leaving the inside of the shape empty. This pattern is created by printing * only at the start and end of each row, while the inner spaces remain blank. The approach consists of two parts: the upper half (increasing pattern) and the lower half (decreasing pattern). Here is the Java program to generate a hollow diamond pattern.
public class HollowDiamondPattern {
public static void main(String[] args) {
int n = 5;
// Printing the upper half of the hollow diamond
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1)) {
System.out.print("*"); // Print star at boundary positions
} else {
System.out.print(" "); // Print space inside the diamond
}
}
System.out.println();
}
// Printing the lower half of the hollow diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1)) {
System.out.print("*"); // Print star at boundary positions
} else {
System.out.print(" "); // Print space inside the diamond
}
}
System.out.println();
}
}
}
This program works similarly to a filled diamond pattern but includes an extra condition within the inner loop. The first and last characters of each row are printed as *, while the rest are spaces. This creates a hollow diamond pattern. The number of stars follows the formula (2 * i - 1), ensuring symmetry in both halves.
*
* *
* *
* *
*********
* *
* *
* *
*
The diamond pattern in Java helps improve coding skills by enhancing loop control, logical thinking, and problem-solving abilities. These patterns can be customized in various ways, such as changing symbols, adjusting sizes, or creating hollow designs. Practicing with different loop structures: for, while, and do-while, provides a more in-depth understanding of iteration.
Use nested loops to print spaces and stars. Increase stars until the middle, then decrease them to form the diamond shape.
Replace the * symbol with any character you prefer in the code.
Change the row count variable; a higher number makes a bigger diamond.
It keeps the design symmetrical and simplifies the looping logic.
Yes, you can use while loops instead of for loops to achieve the same pattern.
Print stars only at the edges by checking row and column positions.
Adjust the leading spaces before the stars to align the pattern properly.
About NxtWave
NxtWave is a learning platform offering industry-recognized certifications and training programs. Contact: [email protected] | WhatsApp: +919390111761