Computer >> Computer tutorials >  >> Programming >> Java

Java Program to Print Diamond Star Pattern


In this article, we will understand how to print Diamond Star pattern. The pattern is formed by using multiple for-loops and print statements.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the number of rows : 8

Output

The desired output would be −

The diamond star pattern :
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *

Algorithm

Step 1 - START
Step 2 - Declare four integer values namely i, j, k and my_input.
Step 3 - Read the required values from the user/ define the values
Step 4 - Assign value of ‘my_input – 1’ to ‘k’
Step 5 - We iterate through two nested 'for' loops to get space between the characters.
Step 6 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character.
Step 7 - Now, print a newline to get the specific number of characters in the subsequent lines.
Step 8 - Display the result
Step 9 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool Java Program to Print Diamond Star Pattern.

import java.util.Scanner;
public class DiamondStar{
   public static void main(String args[]){
      int i, j, k, my_input;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      k = my_input - 1;
      System.out.println("The diamond star pattern : ");
      for (j = 1; j<= my_input; j++){
         for (i = 1; i<= k; i++){
            System.out.print(" ");
         }
         k--;
         for (i = 1; i <= 2 * j - 1; i++){
            System.out.print("*");
         }
         System.out.println("");
      }
      k = 1;
      for (j = 1; j<= my_input - 1; j++){
         for (i = 1; i<= k; i++){
            System.out.print(" ");
         }
         k++;
         for (i = 1; i<= 2 * (my_input - j) - 1; i++){
            System.out.print("*");
         }
         System.out.println("");
      }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number : 8
The diamond star pattern :
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
  *************
   ***********
    *********
     *******
      *****
       ***
        *

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class DiamondStar{
   public static void main(String args[]){
      int i, j, k, my_input;
      my_input = 8;
      k = my_input - 1;
      System.out.println("The number of rows is defined as " +my_input);
      System.out.println("The diamond star pattern : ");
      for (j = 1; j<= my_input; j++){
         for (i = 1; i<= k; i++){
             System.out.print(" ");
         }
         k--;
         for (i = 1; i <= 2 * j - 1; i++){
            System.out.print("*");
         }
         System.out.println("");
      }
      k = 1;
      for (j = 1; j<= my_input - 1; j++){
         for (i = 1; i<= k; i++){
            System.out.print(" ");
         }
         k++;
         for (i = 1; i<= 2 * (my_input - j) - 1; i++){
            System.out.print("*");
         }
         System.out.println("");
      }
   }
}

Output

The number of rows is defined as 8
The diamond star pattern :
      *
     ***
    *****
   *******
  *********
 ***********
 *************
***************
 *************
  ***********
  *********
   *******
    *****
     ***
      *