Star pyramid program in java

Star pyramid program in java





import java.util.Scanner;
public class StarPyramid
{
 public static void main(String[] args) {
  int num;
  Scanner sc = new Scanner(System.in);
  System.out.print(“Enter number of row : “);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = (num – r); sp >= 1; sp–) {
    System.out.print(” “);
   }
   for (int c = 1; c <= (r * 2) – 1; c++) {
    System.out.print(“*”);
   }
   System.out.println();
  }
 }
}


Output:


Enter number of row : 5


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



BUILD SUCCESSFUL (total time: 2 seconds)

LEAVE A REPLY

Please enter your comment!
Please enter your name here