Back

How to Print a Diamond Pattern in Java with Example Code

29 Apr 2025
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.

Diamond Pattern 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.

Why Diamond Patterns Matter in Java Programming?

The diamond pattern in Java is more than just visually attractive. It helps programmers develop essential coding skills. Here’s why they’re important:

  • Mastering Loops: Since diamond patterns depend on nested loops to control rows and columns, they provide a hands-on way to understand loop structures, especially how iterations work in a structured manner.
  • Boosting Logical Thinking: Creating these patterns requires careful placement of spaces and characters, helping programmers strengthen their ability to think logically and break problems into smaller steps.
  • Boosting Creativity: Diamond patterns aren’t rigid; they can be customized by changing their size, using different symbols, or modifying the structure to create unique designs. This allows for experimentation and creative problem-solving.

Basic Diamond Pattern in Java

Algorithm for Diamond Pattern in Java:

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:

  • Print n-i spaces (for alignment).
  • Print 2*i - 1 stars (*).
  • Move to the next line.

3. Bottom Half of the Diamond:

For each row i from n-1 down to 1:

  • Print n-i spaces (for alignment).
  • Print 2*i - 1 stars (*).
  • Move to the next line.

4. Output: The result is a symmetrical diamond pattern printed on the console.

Pseudocode for Diamond Pattern in Java

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

Using a For Loop

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:

Code Example

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();
        }
    }
}

Explanation

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.

Output

    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *   

Time and Space Complexity

  • Time Complexity: O(n²) - Since we use nested loops iterating over n rows and columns.
  • Space Complexity: O(1) - No additional memory is used apart from loop variables.

Using a While Loop

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.

Code Example

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--;
        }
    }
}

Explanation

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.

Output

    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *  

Time and Space Complexity

  • Time Complexity: O(n²) - Due to nested while loops iterating over n rows and columns.
  • Space Complexity: O(1) - Only loop variables are used, requiring no extra memory.

Using Do-While Loop

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.

Code Example

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);
    }
}

Explanation

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.

Output

    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *  

Time and Space Complexity

  • Time Complexity: O(n²) - Due to nested do-while loops iterating over n rows and columns.
  • Space Complexity: O(1) - No extra memory is used beyond loop variables.

Hollow Diamond Pattern in Java

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.

Code Example

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();
        }
    }
}

Explanation

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.

Output

    *    
   * *   
  *   *  
 *     * 
*********
 *     * 
  *   *  
   * *   
    *  

Time and Space Complexity

  • Time Complexity: O(n²) - Due to nested loops iterating over n rows and columns.
  • Space Complexity: O(1) - No additional memory is used apart from loop variables.

Customizing Diamond Patterns

  • Using Different Characters: Instead of using the * symbol, you can experiment with numbers, letters, or other symbols to create unique designs. For example, replacing * with numbers like 1, 2, and 3 or alphabets like A, B, and C can make your pattern more interesting.
  • Adjusting the Size: The size of the diamond is controlled by the number of rows. Increasing this number makes the pattern larger, while decreasing it results in a smaller design.
  • Aligning the Pattern Properly: To make the diamond look centered, adjust the number of spaces before each line. This ensures that the pattern remains symmetrical and visually appealing.

Conclusion

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.

Frequently Asked Questions

1. How do I create a diamond pattern in Java?

Use nested loops to print spaces and stars. Increase stars until the middle, then decrease them to form the diamond shape.

2. How can I change the character used in the diamond pattern?

Replace the * symbol with any character you prefer in the code.

3. How do I customize the size of the diamond pattern?

Change the row count variable; a higher number makes a bigger diamond.

4. What is the purpose of using the middle row in diamond patterns?

It keeps the design symmetrical and simplifies the looping logic.

5. Can I use while loops to create a diamond pattern?

Yes, you can use while loops instead of for loops to achieve the same pattern.

6. How do I create a hollow diamond pattern in Java?

Print stars only at the edges by checking row and column positions.

7. How do I center the diamond pattern on the console?

Adjust the leading spaces before the stars to align the pattern properly.

Read More Articles

Chat with us
Chat with us
Talk to career expert