Diamond Star Pattern in Java

Diamond Star Pattern in Java

In this tutorial, we will learn how to print a diamond star pattern in Java. The pattern combines the logic of both a pyramid and an inverted pyramid to create a symmetric diamond shape.

🔺 What is a Diamond Pattern?

The diamond pattern is a combination of two pyramids (one pointing upwards and one downwards) that forms a diamond shape.

Example for n = 5:

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

✅ Java Program


public class DiamondPattern {
    public static void main(String[] args) {
        int n = 5;

        // Upper half of diamond (pyramid)
        for (int i = 1; i <= n; i++) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }

            // Print stars
            for (int k = 1; k <= (2 * i - 1); k++) {
                System.out.print("*");
            }

            System.out.println();
        }

        // Lower half of diamond (inverted pyramid)
        for (int i = n - 1; i >= 1; i--) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }

            // Print stars
            for (int k = 1; k <= (2 * i - 1); k++) {
                System.out.print("*");
            }

            System.out.println();
        }
    }
}

🧠 Interview Tip

Ensure that you handle both the upper and lower halves of the diamond correctly. The upper half is a pyramid, and the lower half is an inverted pyramid. Understanding the logic of both is key to mastering more complex patterns.

🎯 Output

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

📌 Conclusion

The diamond star pattern is a great exercise to practice nested loops and their logic. By mastering this pattern, you’ll gain better understanding of how to manipulate loops for various complex patterns.