Home » Java programs » Java basic programs

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
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.