Printing a Diamond based on user input.



Program Code :

import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
System.out.print("Enter the number of star = ");
int n=scn.nextInt();
for (int i = 1; i <=n; i=i+2) {
for (int j = n-i; j >=1; j--) {
System.out.print(" ");
}
for (int k = 1; k <=i; k++) {
System.out.print("* ");
}
System.out.println("");
}
for (int i = 1; i <=n-2; i=i+2) {
for (int j = 1; j <=i; j++) {
System.out.print(" ");
}
for (int k = i; k <=n-2; k++) {
System.out.print(" *");
}
System.out.println("");
}
}
}


Output :


 Enter the number of star = 7
         *
      * * *
   * * * * *
* * * * * * *
   * * * * *
      * * *
         *

 
Top