×

Java Programs

Java Practice

Java program to print the diamond shape of stars

In this java program, we are going to learn how to print the diamond shape of stars?
Submitted by IncludeHelp, on November 07, 2017

Here, we are reading number of rows, and according to the input diamond pattern will be printed.

Example:

Input:
Enter number of rows: 10

Output:

         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

Print diamond of stars in java

import java.util.Scanner;

public class Pattern12 
{
	public static void main(String[] args) 
    {
		int n, i, j, space = 1;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter number of rows: ");
		n=s.nextInt();

		space = n - 1;

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

		    space--;

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

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

Output

Enter number of rows: 
10
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.