http://www.technicalpage.net/search/label/SQL

Pyramid of numbers or characters in Java

 

package Interview;

 

public class Pyramid {

 

       public static void main(String[] args) {

 /*

               1

              111

             11111    

     

             i=0, k=1 i is height, k=number of 1s

             i=1, k=3

             i=2, k=5

             //i=3, k=7

*/

             int ht = 5;

             for (int i = 0; i < ht; i++) {

                   

                    for (int j = ht-i-2; j >= 0; j--) {

                           System.out.print(" ");                        

                    }

                    for (int k = 0; k < i*2+1; k++) {

                           System.out.print("1");                        

                    }

                    System.out.print("\n");   

 

             }

       }

 

}

 

Output:

    1

   111

  11111

 1111111

111111111

No comments:

Post a Comment