Tuesday, 20 January 2015

Java Program to print a diamond with stars pattern


This is a simple Java Program which prints out a diamond filled with stars with respect to the input given.

Java Program to print a diamond with stars


PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class DiamondWithStars {
 public static void main(String[] args) {
  int i, j, k, n;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter number of lines[height of diamond] : \t");
  n = scan.nextInt();
  scan.close();
  for (i = 1; i <= n; i++) {
   for (j = i; j < n; j++) {
    System.out.print(" ");
   }
   for (k = 1; k < (i * 2); k++) {
    System.out.print("*");
   }
   System.out.print("\n");
  }
  for (i = n - 1; i >= 1; i--) {
   for (j = n; j > i; j--) {
    System.out.print(" ");
   }
   for (k = 1; k < (i * 2); k++) {
    System.out.print("*");
   }
   System.out.print("\n");
  }
 }
}

OUTPUT : 
Java Program to print a diamond with stars

No comments:

Post a Comment