Java Program to Print Diamond Star Pattern

Write a Java Program to print a diamond star pattern using for loop. The Diamond pattern is a combination of a pyramid and an inverted pyramid. So, this star diamond example divides code to print the normal and inverted pyramid.

package ShapePrograms;

import java.util.Scanner;

public class DiamondPattern1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j;
		
		System.out.print("Enter Diamond Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Diamond Star Pattern");
		
		for (i = 1 ; i <= rows; i++ ) 
		{
			for (j = 1 ; j <= rows - i; j++ ) 
			{
				System.out.print(" ");	
			}
			for (j = 1 ; j <= i * 2 - 1; j++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
		
		for (i = rows - 1 ; i > 0; i-- ) 
		{
			for (j = 1 ; j <= rows - i; j++ ) 
			{
				System.out.print(" ");
			}
			for (j = 1 ; j <= i * 2 - 1; j++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
Java Program to Print Diamond Star Pattern 1

Java Program to Print Diamond Star Pattern using while loop

In this Diamond Pattern of stars program, we replaced the for loop with a while loop.

package ShapePrograms;

import java.util.Scanner;

public class DiamondPattern3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Diamond Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Diamond Star Pattern");
		i = 1 ;
		while(i <= rows ) 
		{
			j = 1 ;
			while( j <= rows - i  ) 
			{
				System.out.print(" ");	
				j++;
			}
			k = 1 ;
			while ( k <= i * 2 - 1 ) 
			{
				System.out.print("*");
				k++;
			}
			System.out.println();
			i++;
		}
		i = rows - 1 ;
		while( i > 0 ) 
		{
			j = 1 ;
			while ( j <= rows - i ) 
			{
				System.out.print(" ");
				j++;
			}
			k = 1 ;
			while( k <= i * 2 - 1 ) 
			{
				System.out.print("*");
				k++;
			}
			System.out.println();
			i--;
		}
	}
}
Enter Diamond Pattern Rows = 12
Printing Diamond Star Pattern
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
 *********************
***********************
 *********************
  *******************
   *****************
    ***************
     *************
      ***********
       *********
        *******
         *****
          ***
           *

Java Program to Print Diamond Star Pattern using do while loop

package ShapePrograms;

import java.util.Scanner;

public class DiamondPattern4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Diamond Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Diamond Star Pattern");
		i = 1 ;
		do
		{
			j = 1 ;
			do
			{
				System.out.print(" ");	
			} while( j++ <= rows - i  ) ;
			k = 1 ;
			do
			{
				System.out.print("*");
			} while ( ++k <= i * 2 - 1 ) ;
			System.out.println();

		} while(++i <= rows ) ;
		
		i = rows - 1 ;
		do
		{
			j = 1 ;
			do
			{
				System.out.print(" ");
			} while (j++ <= rows - i ) ;
			k = 1 ;
			do
			{
				System.out.print("*");
			} while( ++k <= i * 2 - 1 ) ;
			System.out.println();
		} while( --i > 0 ) ;
	}
}
Enter Diamond Pattern Rows = 15
Printing Diamond Star Pattern
               *
              ***
             *****
            *******
           *********
          ***********
         *************
        ***************
       *****************
      *******************
     *********************
    ***********************
   *************************
  ***************************
 *****************************
  ***************************
   *************************
    ***********************
     *********************
      *******************
       *****************
        ***************
         *************
          ***********
           *********
            *******
             *****
              ***
               *

In this example, the diamondPattern function prints the diamond pattern of a given symbol.

package ShapePrograms;

import java.util.Scanner;

public class DiamondPattern2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Diamond Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for Diamond Pattern = ");
		char ch = sc.next().charAt(0);

		diamondPattern(rows, ch);
	}
	public static void diamondPattern(int rows, char ch) {
		int i, j, k;
		int x = rows - 1;
		int y = 1;
		
		for (i = 1 ; i <= rows; i++ ) 
		{
			for (j = 1 ; j <= x; j++ ) 
			{
				System.out.print(" ");	
			}
			for (k = 1 ; k <= y; k++ ) 
			{
				System.out.print(ch);
			}
			if(x > i)
			{
				x = x - 1;
				y = y + 2;
			}
			if(x < i)
			{
				x = x + 1;
				y = y - 2;
			}
			System.out.println();
		}
	}
}
Enter Diamond Pattern Rows = 10
Enter Character for Diamond Pattern = #
         #
        ###
       #####
      #######
     #########
     #########
      #######
       #####
        ###
         #

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.