Q:

Java program to print the diamond shape of stars

0

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:

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now