Print Asterisk Pyramid in Java
Below code will print the asterisk pyramid upto 5 level
public class AsteriskPyramid { public static void main(String[] args) { int a = 1; for (int i = 1; i <= 5; i++) { for (int j = i; j < 5; j++) { System.out.print(" "); } for (int k = 1; k <= a; k++) { if (k % 2 == 0) System.out.print(" "); else System.out.print("*"); } System.out.println(); a += 2; } } }
in the above program we are initializing three loops, our basic requirement is to print the pyramid up to 5 label, so first loop will run up to 5, second loop inside first loop will also be run up to 5 label, in the third loop the program will print the space and asterisk on certain condition match there are two conditions in if condition the program will check that remainder of k should be equal to zero then it will print ” ” else it will print the”*”.