Java Program for Diamond Star Pattern

Printing Diamond Star Pattern

In this program we’re going to code diamond star pattern program .

In this program we’re taking input from user and store in the variable names as n and then we will run a for loop start from j=n to j>i+1 and then print spaces after that take a j from loop again start from j=0 to j<=i*2 after that print star statement and then again take another for loop start from i=n to i>0 and then take a for loop star from k=n to k– and then print spaces after that take another loop to print star

Java Program for Diamond Star Pattern

Algorithm:

  • Enter the number input from the user and store it in any variable.(‘n‘ in this case).
  • Run a loop ‘n’ number of times to iterate through each of the rows. From i=0 to i<n. The loop should be structured as for( i=0 ; i
  • Inside main loop take another loop start from j=n to  j– and then print System.out.println(“”);
  • After this inside main loop take another loop to print stars start from  j=0 to j++ and then print star System.out.println(“*”)
  • After 1st mail loop take another 2nd main loop start from i=n to i–
  • Inside this take 1st inner loop start from k=n to k– and then print spaces  System.out.print(” “);
  • After this take another inner loop to print stars start from j=0 to j++ System.out.print(“*”);

Code in Java:

import java.util.Scanner;
public class Pattern1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter No");
		int n=sc.nextInt();
		 for (int i=0; i<n; i++){ 
             for (int j=n; j>i+1; j--) 
               System.out.print(" "); 
             for (int j=0; j<=i*2; j++ ) 
               System.out.print("*"); 
          System.out.println(); 
      } 
		for(int i=n;i>0;i--)     
		   {
		      for(int k=n;k>i;k--)     
		         System.out.print(" ");    
		      for(int j=0;j<i*2-1;j++)     
		         System.out.print("*");    
		      System.out.println();     
		   }
	 }

}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)

10 comments on “Java Program for Diamond Star Pattern”


  • Abhishek Kole

    import java.util.Scanner;
    public class Patterns{
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter number : “);
    int row = sc.nextInt();

    for(int i=1; i<=row; i++) {
    for(int j=1; j<=row-i; j++)
    System.out.print(" ");
    for(int j=1; j=1; i–) {
    for(int j=row-1; j>=i; j–)
    System.out.print(” “);
    for(int j=1; j<=2*i-1; j++)
    System.out.print("*");
    System.out.println();
    }
    }
    }


  • abhaydwivedi4504

    package patternPrinting;
    import java.util.Scanner;

    public class DiamondStarPattern {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt(); // n represents the number of rows for the upper half (excluding the middle line)

    // Combined loop for upper and lower halves
    for (int i = 0; i < 2 * n – 1; i++) {
    int currentRow = i < n ? i : 2 * n – 2 – i; // Mirror row index for the lower half

    // Printing spaces
    for (int j = 0; j < n – currentRow – 1; j++) {
    System.out.print(" ");
    }

    // Printing stars
    for (int j = 0; j < 2 * currentRow + 1; j++) {
    System.out.print("*");
    }

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


  • Surya

    import java.util.*;
    class daimondpattern
    {
    public static void main(String ar[])
    {
    int i,j;
    Scanner sc=new Scanner(System.in);
    int n=sc.newInt();
    int l;
    for(i=-n;i<=n;i++)
    {
    if(i<0) {
    l=-i;
    }
    else
    {
    l=i;
    }
    for(j=0;j<l;j++)
    {
    System.out.print(" ");
    }
    for(j=0;j<n-l+1;j++)
    {
    System.out.print("* ");
    }
    System.out.println();
    }
    }
    }


  • Pulivendula

    import java.util.*;

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


  • manicampusplacements

    import java.util.*;
    public class Main{
    public static void main(String[] args){
    int n = new Scanner(System.in).nextInt();
    int count = 1;
    for(int i=1;i<=n;i++){
    for(int j=1;j<=n-i;j++) System.out.print(" ");
    for(int k=1;k=1;i–){
    for(int j=1;j<=n-i;j++) System.out.print(" ");
    for(int k=1;k<=count;k++) System.out.print("*");
    count -= 2;
    System.out.println();
    }
    }
    }


  • jiteshghormade28

    import java.util.Scanner;

    public class DiamondPattern5 {
    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.print(“Enter No of Rows : “);
    int r = sc.nextInt();

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

    }


  • Keerthick

    #java
    public static void main(String[] args) {
    int n = 5;
    for(int i=1; i=i; j–){
    System.out.print(” “);
    }
    for(int j=1; j<=i; j++){
    System.out.print("* ");
    }
    for(int j=1; j<i; j++){
    System.out.print("* ");
    }
    System.out.println();
    }
    for(int i=1; i<=n; i++){
    for(int j=1; j=i; j–){
    System.out.print(“* “);
    }
    for(int j=n-1; j>i; j–){
    System.out.print(“* “);
    }
    System.out.println();
    }
    }


    • abhaydwivedi4504

      package patternPrinting;
      import java.util.Scanner;

      public class DiamondStarPattern {
      public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt(); // n represents the number of rows for the upper half (excluding the middle line)

      // Combined loop for upper and lower halves
      for (int i = 0; i < 2 * n – 1; i++) {
      int currentRow = i < n ? i : 2 * n – 2 – i; // Mirror row index for the lower half

      // Printing spaces
      for (int j = 0; j < n – currentRow – 1; j++) {
      System.out.print(" ");
      }

      // Printing stars
      for (int j = 0; j < 2 * currentRow + 1; j++) {
      System.out.print("*");
      }

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