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)

6 comments on “Java Program for Diamond Star Pattern”


  • 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();
    }
    }