Thursday, January 22, 2015

Java Pyramid Example – Pattern 11


In the program the following star pattern will be printed.


The program is shown below :



package com.skilledmonster.examples.loops;
 
public class PyramidNestedForLoopExample_11 {
 
    public static void main(String[] args) {
 
        // levels in the pyramid
        int x = 5;
 
        for (int i = 1; i <= x; i++) {
            // for spacing
            for (int j = 1; j <= x - i; j++){                System.out.print("   ");            }           // left half of the pyramid             for (int k = i; k >= 1; k--){
                System.out.print((k >= 10) ? +k : "  " + k);
            }
            // corresponding right half of the pyramid
            for (int k = 2; k <= i; k++) {               System.out.print((k >= 10) ? +k : "  " + k);
            }
            // next line
            System.out.println();
        }
    }
 

}



Output :


No comments:

Post a Comment