Reverse star pyramid program in Java





package demo;
import java.util.Scanner;
public class ReverseStar
{
 public static void main(String[] args) 
 {
  int num, r = 1;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter loop repeat number(rows): ");
  num = sc.nextInt();
  System.out.println();
  for (; num >= 1; num--, r++) 
  {
   for (int c = 1; c <= num; c++) {
    System.out.print("*");
   }
   for (int sp = r; sp > 1; sp--) {
    System.out.print(" ");
   }
   for (int sp = r; sp > 1; sp--) {
    System.out.print(" ");
   }
   for (int c = num; c >= 1; c--) {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

Output:

Enter loop repeat number(rows): 7

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


BUILD SUCCESSFUL (total time: 3 seconds)




No comments:

Post a Comment