Monday, December 21, 2015

Java Program to show diamond Pattern:

Java Program to show diamond Pattern:

import java.util.Scanner;
public class MyDiamondPattern {
  public static void main(String[] args) {
    System.out
      .print("Enter the number of stars you want in diamond shape: ");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    System.out.println("Diamond of Stars are \n");
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < (n - i); j++)
        System.out.print("  ");
      for (int j = 1; j <= i; j++)
        System.out.print(" *");
      for (int k = 1; k < i; k++)
        System.out.print(" *");
      System.out.println();
    }
 
    for (int i = n - 1; i >= 1; i--) {
      for (int j = 0; j < (n - i); j++)
        System.out.print("  ");
      for (int j = 1; j <= i; j++)
        System.out.print(" *");
      for (int k = 1; k < i; k++)
        System.out.print(" *");
      System.out.println();
    }
 
    System.out.println();
  }

}
The output of this program is below:
run:
Enter the number of stars you want in diamond shape: 7
Diamond of Stars are

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



0 comments:

Post a Comment