Thursday, January 22, 2015

Java Pyramid Example – Pattern 4


In the program the following star pattern will be printed.


The program is shown below :




package com.skilledmonster.examples.loops;
 
public class PyramidNestedForLoopExample_4 {
 
    public static void main(String[] args) {
 
        // generate first-half of the pyramid
        for(int i=5; i>0 ;i--){
 
            for(int j=0; j < i; j++){
                // display/add star
                System.out.print("*");
            }
 
            //create a new line
            System.out.println("");
        }// end of for loop
 
        // generate second-half of the pyramid
        for(int i=1; i<= 5 ;i++){
 
            for(int j=0; j < i; j++){
                // display/add star
                System.out.print("*");
            }
 
            //create a new line
            System.out.println("");
        }// end of for loop
 
    }
 

}

Output :


No comments:

Post a Comment