Java Program to Print Pyramid Star Pattern

·

1 min read

In this program, you'll learn to create pyramids, half pyramids, inverted pyramids, Pascal's triangle and Floyd's triangle using control statements in Java.

Example 1: Program to print half pyramid using *

*
* *
* * *
* * * *
* * * * *

Source Code:

int n = 5;
// outer loop
for(int i = 1; i<=n; i++){
//inner loop
for(int j= 1; j<=i; j++){
Sysyetm.out.print("*");
}
System.out.println();
}

Explanation:

i = 1 when as j = 1 a star * will be printed j increases when j = 2 then that does not print anything because of the condition of the inner loop. In the next line i = 2 when i =2 then we go inner loop then first j = 1 then the star will print then j increment j = 2 then one starts printing on the same line we go to the next line i = 3 or j = 1 and j increment j = 2 or j = 3 when our inner loop will run until the value of j is equal to I then i increment to reach i =6 then the loop will not work because 6 is not less than equal to n