Nested Star Hash Pyramid Program in Java





package demo;
public class NestedStrarPyramid
{
 public static void main(String[] args) 
 {
  int n = 5;
  for (int r = 1; r <= 6; r++, n--) {
   /*first pyramid*/
   for (int c = 1; c <= n; c++) {
    System.out.print(" #");
   }
   /*second pyramid*/
   for (int c = 1; c <= r; c++) {
    if (c == 1) {
     System.out.print(" *");
    } else {
     System.out.print(" #");
    }
   }
   /*third pyramid*/
   for (int c = r; c > 1; c--) {
    if (c == 2) {
     System.out.print(" *");
    } else {
     System.out.print(" #");
    }
   }
   /*fourth pyramid*/
   for (int c = n; c >= 1; c--) {
    System.out.print(" #");
   }
   System.out.println();
  }
 }
}

Output:

run:

 # # # # # * # # # # #
 # # # # * # * # # # #
 # # # * # # # * # # #
 # # * # # # # # * # #
 # * # # # # # # # * #
 * # # # # # # # # # *


BUILD SUCCESSFUL (total time: 1 second)


No comments:

Post a Comment