In this tutorial, we will see java program to print diamond pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.Scanner; public class DiamondPattPrinter { public static void main(String args[]) { int cnt, c, k, space=1; Scanner scan = new Scanner(System.in); System.out.print("Enter count/number of Rows for Diamond pattern : "); cnt = scan.nextInt(); space = cnt-1; for(k=1; k<=cnt; k++){ for(c=1; c<=space; c++){ System.out.print(" "); } space--; for(c=1; c<=(2*k-1); c++){ System.out.print("*"); } System.out.println(); } space = 1; for(k=1; k<=(cnt-1); k++){ for(c=1; c<=space; c++){ System.out.print(" "); } space++; for(c=1; c<=(2*(cnt-k)-1); c++){ System.out.print("*"); } System.out.println(); } } } |
That’s all about printing diamond pattern in java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.