Monday, 2 November 2015

Pyramid of stars using nested for loops



Its a very common program asked to do during our course of Java.This program is very good to understand how loops works in Java.


Input:- 5

Ouptut:-    *
                **
                ***
                ****
                *****


Sample Program:-

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


Output  
 *  
 **  
 ***  
 ****  
 *****  



Enjoy Programming

No comments:

Post a Comment