This is a simple Java Program which prints out a diamond filled with stars with respect to the input given.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNgSu71tfqySmXkZWrkMJU3HoEzs0AuIZF9hofYIbd2dQzMAO4I1dFyoE3-EjWLRPEjgF9hjG22E_JzmI06C8d52ZCBJt5EN03Z7XBStC-Js0lK4mf-MmtZKePisoWihr09PN7mRcmjF8/s1600/diamond.PNG) |
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 : ![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6hf82ZH7jzKFv7I6HDOfmhKhgFgul0Yq7-BPSFlgd6XkU3pv5OO22quoLG7SUz59jGP7ujMhFSLw7DR9_gK7b3J_0Hy0e_Py1lpTj7SVlmf0rK5Dos3HHew6lUcrY_S_zHGCKoC50Eq8/s1600/diamondProg.PNG) |
Java Program to print a diamond with stars |
No comments:
Post a Comment