learn2code

Diamond Pattern in Java – 2024 Interview Questions

Diamond Pattern In Java

Creating a diamond pattern is a common exercise for learning nested loops and conditional statements in Java. This blog post will walk you through the steps to create a diamond pattern and explain each part of the code.

1. Diamond Pattern in Java

Code

public class DiamondPattern {
    public static void main(String[] args) {
        int n = 5; // Number of rows
        for (int i = 1; i <= n; i++) {
            for (int j = i; j < n; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k < (i * 2); k++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = n - 1; j >= i; j--) {
                System.out.print(" ");
            }
            for (int k = 1; k < (i * 2); k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Explanation

  1. First Loop: The outer loop (for (int i = 1; i <= n; i++)) controls the number of rows for the top half of the diamond. The variable n represents the total number of rows in the top half.
  2. Inner Loop 1: The first inner loop (for (int j = i; j < n; j++)) prints the spaces before the stars. As i increases, the number of spaces decreases, moving the stars to the right.
  3. Inner Loop 2: The second inner loop (for (int k = 1; k < (i * 2); k++)) prints the stars. The number of stars increases as i increases, forming the top half of the diamond.
  4. New Line: After printing each row of spaces and stars, System.out.println() moves the cursor to the next line.
  5. Second Loop: The second outer loop (for (int i = n – 1; i >= 1; i–)) controls the number of rows for the bottom half of the diamond. It starts from n – 1 and decrements i until it reaches 1.
  6. Inner Loop 3: Similar to the first inner loop, this loop (for (int j = n – 1; j >= i; j–)) prints spaces before the stars for the bottom half.
  7. Inner Loop 4: Similar to the second inner loop, this loop (for (int k = 1; k < (i * 2); k++)) prints stars for the bottom half of the diamond.
  8. New Line: System.out.println() is called again to move to the next line after each row of the bottom half is printed.

2. Hollow Diamond Pattern in Java

A hollow diamond pattern is a variation where the diamond’s middle part is hollow. This exercise helps in understanding the use of conditionals within loops.

Code

public class HollowDiamondPattern {
    public static void main(String[] args) {
        int n = 5; // Number of rows
        for (int i = 1; i <= n; i++) {
            for (int j = i; j < n; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k < (i * 2); k++) {
                if (k == 1 || k == (i * 2 - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = n - 1; j >= i; j--) {
                System.out.print(" ");
            }
            for (int k = 1; k < (i * 2); k++) {
                if (k == 1 || k == (i * 2 - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

Explanation

  1. First Loop: The outer loop (for (int i = 1; i <= n; i++)) handles the rows for the top half.
  2. Inner Loop 1: Prints spaces (for (int j = i; j < n; j++)) to align the stars.
  3. Inner Loop 2: This loop (for (int k = 1; k < (i * 2); k++)) prints stars and spaces. The if condition checks if the current position is at the border of the diamond (k == 1 || k == (i * 2 – 1)). If true, it prints a star; otherwise, it prints a space.
  4. New Line: Moves to the next line after printing each row.
  5. Second Loop: Handles the rows for the bottom half (for (int i = n – 1; i >= 1; i–)).
  6. Inner Loop 3: Prints spaces for the bottom half.
  7. Inner Loop 4: Similar to the second inner loop, it prints stars at the border positions and spaces otherwise.

3.Half Diamond Pattern in Java

Creating a half diamond pattern is a great way to understand nested loops and conditional statements. In this exercise, we will create a half diamond pattern in Java.

Code

public class HalfDiamondPattern {
public static void main(String[] args) {
int n = 5; // Number of rows for the upper half of the diamond

public class HalfDiamondPattern {
    public static void main(String[] args) {
        int n = 5; // Number of rows for the upper half of the diamond

        // Upper half of the diamond
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Lower half of the diamond
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
*
**
***
****
*****
****
***
**
*

Explanation

  1. Upper Half: The first for loop (for (int i = 1; i <= n; i++)) controls the rows of the upper half. The inner for loop (for (int j = 1; j <= i; j++)) prints the stars. As i increases, the number of stars printed in each row also increases.
  2. Lower Half: The second for loop (for (int i = n – 1; i >= 1; i–)) controls the rows of the lower half. The inner for loop (for (int j = 1; j <= i; j++)) prints the stars. As i decreases, the number of stars printed in each row also decreases.
  3. New Line: System.out.println() is called after each row to move the cursor to the next line, ensuring the stars are printed on new lines for each row.

4.Alphabet Diamond Pattern in Java

Creating an alphabet diamond pattern is a fascinating way to explore nested loops and character manipulation in Java. This exercise involves printing a diamond pattern with alphabetic characters.

Code

public class AlphabetDiamondPattern {
    public static void main(String[] args) {
        int n = 5; // Number of rows for the upper half of the diamond
        char ch = 'A';

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

            // Print increasing alphabet sequence
            for (int j = 0; j <= i; j++) {
                System.out.print((char) (ch + j));
            }

            // Print decreasing alphabet sequence
            for (int j = i - 1; j >= 0; j--) {
                System.out.print((char) (ch + j));
            }

            // Move to the next line
            System.out.println();
        }

        // Lower half of the diamond
        for (int i = n - 2; i >= 0; i--) {
            // Print leading spaces
            for (int j = n - i; j > 1; j--) {
                System.out.print(" ");
            }

            // Print increasing alphabet sequence
            for (int j = 0; j <= i; j++) {
                System.out.print((char) (ch + j));
            }

            // Print decreasing alphabet sequence
            for (int j = i - 1; j >= 0; j--) {
                System.out.print((char) (ch + j));
            }

            // Move to the next line
            System.out.println();
        }
    }
}
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A

Explanation

  1. Upper Half: The first for loop (for (int i = 0; i < n; i++)) controls the rows for the upper half. The nested loops manage spaces and characters.
    • Leading Spaces: The first nested loop (for (int j = n – i; j > 1; j–)) prints spaces to align the characters in a diamond shape.
    • Increasing Alphabet Sequence: The second nested loop (for (int j = 0; j <= i; j++)) prints the increasing sequence of alphabets.
    • Decreasing Alphabet Sequence: The third nested loop (for (int j = i – 1; j >= 0; j–)) prints the decreasing sequence of alphabets.
  2. Lower Half: The second for loop (for (int i = n – 2; i >= 0; i–)) controls the rows for the lower half.
    • Leading Spaces: The first nested loop prints spaces.
    • Increasing Alphabet Sequence: The second nested loop prints the increasing sequence of alphabets.
    • Decreasing Alphabet Sequence: The third nested loop prints the decreasing sequence of alphabets.
  3. Character Casting: The expression (char) (ch + j) converts the integer result back to a character, creating the alphabet sequence.

5.Diamond Number Pattern Programs in Java

Creating a diamond number pattern is an excellent way to practice nested loops and conditional statements in Java. This exercise involves printing a diamond pattern using numbers.

Code

public class DiamondNumberPattern {
    public static void main(String[] args) {
        int n = 5; // Number of rows for the upper half of the diamond

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

            // Print increasing number sequence
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }

            // Print decreasing number sequence
            for (int j = i - 1; j >= 1; j--) {
                System.out.print(j);
            }

            // Move to the next line
            System.out.println();
        }

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

            // Print increasing number sequence
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }

            // Print decreasing number sequence
            for (int j = i - 1; j >= 1; j--) {
                System.out.print(j);
            }

            // Move to the next line
            System.out.println();
        }
    }
}
    1
   121
  12321
 1234321
123454321
 1234321
  12321
   121
    1

Explanation

  1. Upper Half: The first for loop (for (int i = 1; i <= n; i++)) controls the rows for the upper half of the diamond. The nested loops manage spaces and numbers.
    • Leading Spaces: The first nested loop (for (int j = n; j > i; j–)) prints spaces to align the numbers in a diamond shape.
    • Increasing Number Sequence: The second nested loop (for (int j = 1; j <= i; j++)) prints the increasing sequence of numbers.
    • Decreasing Number Sequence: The third nested loop (for (int j = i – 1; j >= 1; j–)) prints the decreasing sequence of numbers.
  2. Lower Half: The second for loop (for (int i = n – 1; i >= 1; i–)) controls the rows for the lower half of the diamond.
    • Leading Spaces: The first nested loop prints spaces.
    • Increasing Number Sequence: The second nested loop prints the increasing sequence of numbers.
    • Decreasing Number Sequence: The third nested loop prints the decreasing sequence of numbers.

6.Diamond in Square Quilt Pattern in Java

Creating a diamond within a square quilt pattern is a more complex exercise that combines nested loops and conditional statements. This pattern involves printing a diamond shape within a larger square.

Code

public class DiamondInSquareQuiltPattern {
    public static void main(String[] args) {
        int n = 7; // Size of the square

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (j == (n / 2 + 1) + (i - (n / 2 + 1)) || 
                    j == (n / 2 + 1) - (i - (n / 2 + 1)) ||
                    j == (n / 2 + 1) - ((n / 2 + 1) - i) ||
                    j == (n / 2 + 1) + ((n / 2 + 1) - i)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
   *   
  * *  
 *   * 
*     *
 *   * 
  * *  
   *   

Explanation

  1. Square Size: The variable n defines the size of the square. For this pattern, n should be an odd number to ensure the diamond fits symmetrically within the square.
  2. Nested Loops: The outer loop (for (int i = 1; i <= n; i++)) controls the rows, while the inner loop (for (int j = 1; j <= n; j++)) controls the columns.
  3. Conditional Statement: The if statement checks the conditions to print a star (*). The conditions are designed to place stars at the correct positions to form the diamond shape within the square:
    • j == (n / 2 + 1) + (i – (n / 2 + 1)): This condition checks for the upper-right diagonal of the diamond.
    • j == (n / 2 + 1) – (i – (n / 2 + 1)): This condition checks for the upper-left diagonal of the diamond.
    • j == (n / 2 + 1) – ((n / 2 + 1) – i): This condition checks for the lower-left diagonal of the diamond.
    • j == (n / 2 + 1) + ((n / 2 + 1) – i): This condition checks for the lower-right diagonal of the diamond.
  4. Spaces: If none of the conditions are met, the program prints a space () to fill the rest of the square.
  5. New Line: After printing each row, System.out.println() moves the cursor to the next line to continue printing the next row.

This code demonstrates how nested loops and conditional logic can be used to create complex patterns in Java. Understanding this pattern will enhance your problem-solving skills and ability to manipulate loops and conditionals.

For more star pattern programs, check out this 10 Star Java Pattern Program Interview Questions. Additionally, explore more number patterns at Number Patterns Program Interview Questions.

Latest Blog Post

Leave a Reply

Harish

Typically replies within a hours