Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource

Table of Contents

What is Diamond Pattern in Java

There are various fascinating patterns that developers often encounter in the world of Java programming. One such intriguing pattern is the Diamond Pattern. Among the myriad of patterns, the Diamond Pattern stands out as an elegant and intriguing design. As the name suggests, this pattern creates a shape resembling a sparkling Diamond, making it visually appealing and aesthetically pleasing. 

Understanding how to create this pattern not only enhances a programmer's coding skills but also provides a creative outlet for visually appealing outputs. Whether used for decorative purposes or educational demonstrations, the Diamond Pattern remains an engaging element in the Interface of Java programming. 

In this blog, we will explore what the Diamond Pattern is, how it works, and how it can be implemented in Java. Diamond Pattern in Java is a straightforward Java program that can be used to print Diamond star patterns.  

Table of Contents

1) What is Diamond Pattern in Java? 

2) How to print a Diamond Pattern in Java 

     a) Diamond star pattern 

     b) Hollow Diamond star pattern 

     c) Number Diamond Pattern 

      d) Alphabet Diamond Pattern 

3) Conclusion 

What is Diamond Pattern in Java? 

The Diamond Pattern is a series of elements, typically stars (*), arranged in a way that forms a rhombus or Diamond-like shape. It is often used for decorative purposes, creating visually stunning outputs that catch the eye. The pattern's unique attribute lies in its symmetrical nature, which gives it a sense of balance and harmony. 

In its most basic form, the Diamond Pattern consists of two main parts: an upper pyramid and a lower pyramid, separated by a middle row of elements. The number of rows in the pattern determines the size of the Diamond, with each row representing a step towards the pattern's widest point and then narrowing down to the other end. 

Try out our Java Training Course for a successful career! 

How to print a Diamond Pattern in Java

Creating a Diamond Pattern in Java is an interesting coding exercise that involves the use of nested loops to achieve a symmetrical and visually appealing design. By carefully manipulating the loops and control structures, we can generate a Diamond-like shape composed of stars (*), which is not only a fascinating decorative element but also a great way to improve our programming skills. In this section, we will walk through the step-by-step process of printing a Diamond Pattern in Java. 

Diamond star pattern 

To construct a Diamond Pattern, we can follow a systematic approach that involves breaking down the problem into smaller steps. The key concept is to determine the number of rows in the pattern based on user input or a fixed value. We divide the pattern generation into two parts: the upper pyramid and the lower pyramid, with a middle row of elements connecting them. 

Code Implementation 

Below is the Java code for printing a Diamond Pattern based on the algorithm: 

 import java.util.Scanner;

public class DiamondPattern {

    public static void main(String[] args) { 

        Scanner sc = new Scanner(System.in); 

        System.out.print("Enter the size of the diamond: "); 

        int size = sc.nextInt();

        // Printing the upper pyramid 

        for (int i = 1; i <= size; i++) { 

            for (int j = size; j > i; j--) { 

                System.out.print(" "); 

            } 

            for (int k = 1; k <= (2 * i - 1); k++) { 

                System.out.print("*"); 

            } 

            System.out.println(); 

        }  

        // Printing the lower pyramid 

        for (int i = size - 1; i >= 1; i--) { 

            for (int j = size; j > i; j--) { 

                System.out.print(" "); 

            } 

            for (int k = 1; k <= (2 * i - 1); k++) { 

                System.out.print("*"); 

            } 

            System.out.println(); 

        } 

    } 


Sample output 

For the user input of size 5, the output will be: 

   

          * 

        *** 

         ***** 

          ******* 

           ********* 

           ******* 

          ***** 

           *** 

           * 

Hollow star Diamond Pattern 

While constructing the Hollow Star Diamond Pattern, we can follow a similar approach to the standard Diamond Pattern with a slight modification. The key difference lies in determining when to print spaces and stars to achieve the desired "hollow" effect. 

Code implementation
 

  import java.util.Scanner;

public class HollowStarDiamondPattern {

    public static void main(String[] args) { 

        Scanner sc = new Scanner(System.in); 

        System.out.print("Enter the size of the diamond: "); 

        int size = sc.nextInt(); 

        // Printing the upper pyramid 

        for (int i = 1; i <= size; i++) { 

            for (int j = size; j > i; j--) { 

                System.out.print(" "); 

            } 

            for (int k = 1; k <= (2 * i - 1); k++) { 

                if (k == 1 || k == (2 * i - 1)) { 

                    System.out.print("*"); 

                } else { 

                    System.out.print(" "); 

                } 

            } 

            System.out.println(); 

        } 

        // Printing the lower pyramid 

        for (int i = size - 1; i >= 1; i--) { 

            for (int j = size; j > i; j--) { 

                System.out.print(" "); 

            } 

            for (int k = 1; k <= (2 * i - 1); k++) { 

                if (k == 1 || k == (2 * i - 1)) { 

                    System.out.print("*"); 

                } else { 

                    System.out.print(" "); 

                } 

            } 

            System.out.println(); 

        } 

    } 

 

Sample output 

For the user input of size 5, the output will be: 
 

         * 

     *  * 

   *      * 

   *          *            

 *             * 

  *         * 

    *     * 
      * * 

       * 

Number Diamond Pattern 

The Number Diamond Pattern is an exciting variation of the classic Diamond Pattern, where numeric sequences are used instead of stars (*). This unique pattern not only retains the symmetrical Diamond-like shape but also adds a mathematical element to it. By replacing stars with numbers, we can create intriguing designs and explore the creative side of Java programming. 

To generate the Number Diamond Pattern, we follow a similar approach to the standard Diamond Pattern, but instead of printing stars, we print numbers in a sequential order. The key concept is to determine the number of rows in the pattern and calculate the numbers to be displayed for each row. 

Code implementation
 

 import java.util.Scanner; 

  public class NumberDiamondPattern { 

      public static void main(String[] args) { 

        Scanner sc = new Scanner(System.in); 

        System.out.print("Enter the size of the diamond: "); 

        int size = sc.nextInt(); 

                 // Printing the upper pyramid 

        for (int i = 1; i <= size; i++) { 

            // Print spaces 

            for (int j = size; j > i; j--) { 

                System.out.print(" "); 

            } 

            // Print numbers in ascending order 

            for (int k = 1; k <= (2 * i - 1); k++) { 

                System.out.print(k); 

            } 

            System.out.println(); 

        } 

    // Printing the lower pyramid 

        for (int i = size - 1; i >= 1; i--) { 

            // Print spaces 

            for (int j = size; j > i; j--) { 

                System.out.print(" "); 

            } 

            // Print numbers in descending order 

            for (int k = 1; k <= (2 * i - 1); k++) { 

                System.out.print(k); 

            } 

            System.out.println(); 

        } 

    } 


Sample output 

For the user input of size 5, the output will be: 
 

        1 

   123 

  12345 

 1234567 

123456789 

 1234567 

  12345 

   123 

    1 

 

Alphabet Diamond Pattern 

The Alphabet Diamond Pattern is yet another captivating variation of the classic Diamond Pattern, where instead of stars or numbers, we use alphabets in a sequential order. This unique pattern allows us to create intriguing designs that showcase the versatility of Java programming. 

Code implementation
 

import java.util.Scanner; 

public class AlphabetDiamondPattern { 

    public static void main(String[] args) { 

        Scanner sc = new Scanner(System.in); 

        System.out.print("Enter the size of the diamond: "); 

        int size = sc.nextInt(); 

        sc.close(); 

        // Printing the upper pyramid 

        for (int i = 0; i < size; i++) { 

            // Print spaces 

            for (int j = size - i - 1; j > 0; j--) { 

                System.out.print(" "); 

            } 

            // Print alphabets in ascending order 

            for (int k = 0; k <= i; k++) { 

                System.out.print((char)('A' + k)); 

            } 

            // Print alphabets in descending order 

            for (int l = i - 1; l >= 0; l--) { 

                System.out.print((char)('A' + l)); 

            } 

            System.out.println(); 

        } 

        // Printing the lower pyramid 

        for (int i = size - 2; i >= 0; i--) { 

            // Print spaces 

            for (int j = size - i - 1; j > 0; j--) { 

                System.out.print(" "); 

            } 

            // Print alphabets in ascending order 

            for (int k = 0; k <= i; k++) { 

                System.out.print((char)('A' + k)); 

            } 

            // Print alphabets in descending order 

            for (int l = i - 1; l >= 0; l--) { 

                System.out.print((char)('A' + l)); 

            } 

            System.out.println(); 

        } 

    } 

}


Sample output 

For the user input of size 5, the output will be: 
 

            A 

     ABA 

    ABCBA 

   ABCDCBA 

  ABCDEDCBA 

   ABCDCBA  

    ABCBA 

     ABA 

      A 

 

Java Training

 

Conclusion 

The Diamond Pattern in Java is a captivating and versatile design that showcases the elegance of nested loops and symmetry. Understanding how to create this pattern not only enhances a programmer's coding skills but also provides a creative outlet for visually appealing outputs. Whether used for decorative purposes or educational demonstrations, the Diamond Pattern remains an engaging element in the world of Java programming. 

Frequently Asked Questions

Upcoming Programming & DevOps Resources Batches & Dates

Date

building Java Programming

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

BIGGEST
NEW YEAR SALE!

red-starWHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.