Java Program to Print Diamond Pattern

star pattern 2

In this tutorial, we will write a java program to print diamond pattern of stars. Before that, you should have knowledge on the following topic in Java.


Java Program to Print Diamond Pattern

Source Code: the program asks the user for the input of a number of rows for the diamond star pattern in java. Then using for loop, the diamond shape pattern is displayed on the screen.

// Print Diamond star Pattern in java

import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    int n, j, i, space = 1;
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter the no. of Rows: ");
    n = scan.nextInt();

    space = n - 1;

    for (i = 1; i <= n; i++)
    {
      for (j = 1; j <= space; j++)
      {
        System.out.print(" ");
      }

      space--;
      for (j = 1; j <= (2 *i - 1); j++)
      {
        System.out.print("*");
      }

      System.out.println();
    }

    space = 1;

    for (i = 1; i <= (n - 1); i++)
    {
      for (j = 1; j <= space; j++)
      {
        System.out.print(" ");
      }

      space++;
      for (j = 1; j <= (2 *(n - i) - 1); j++)
      {
        System.out.print("*");
      }

      System.out.println();
    }
  }
}

Output:

java diamond pattern of star