Monday, 2 November 2015

Reversed pyramid using for loops and decrement operator


This Java pyramid example shows how to generate pyramid or triangle like given below using for loop.

12345
1234
123
12
1


Sample Program:-

 /*  
  This Java Pyramid example shows how to generate pyramid or triangle  
  like given below using for loop.  
  12345  
  1234  
  123  
  12  
  1  
  *   
  *  
  */  
 public class ReversePyramid {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           for (int i = 5; i > 0; i--) {  
                for (int j = 0; j < i; j++) {  
                     System.out.print(j + 1);  
                }  
                System.out.println("");  
           }  
      }  
 }  


 Output  
 12345  
 1234  
 123  
 12  
 1  


Enjoy Programming

No comments:

Post a Comment