To print Pyramid Star Pattern in java, we perform following step :-
- Create Scanner class Object to take input parameter from user.
- We need to use two loops, first outer loop is responsible for print the rows and the second inner loop is responsible for print columns. space.
- The first Row will have only 1 star, every other row will have No. of stars = Stars in Previous Row + 2.
import java.util.Scanner;
public class PyramidStar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows:- ");
int row = sc.nextInt();
for (int i = 1; i <= row; i++) {
for (int j = (row - i); j >= 1; j--) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:-
Enter the number of rows:- 6
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *