Thursday, January 22, 2015

Java Pyramid Example – Pattern 7


In the program the following star pattern will be printed.



The program is shown below :



package com.skilledmonster.examples.loops;
 
public class PyramidNestedForLoopExample_7 {
 
    public static void main(String[] args) {
 
        for (int i = 1; i <= 5; i++) {
            for (int s = 5; s > i; s--) {
                // add spacing
                System.out.print(" ");
            }
            for (int j = 1; j < i; j++) {
                //display/add star
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                //display/add star
                System.out.print("*");
            }
            // add new line
            System.out.println("");
        }
        for (int i = 1; i <= 5; i++) {
            for (int s = 1; s < i; s++) {
                // add spacing
                System.out.print(" ");
            }
            for (int j = 5; j > i; j--) {
                //display/add star
                System.out.print("*");
            }
            for (int j = 5; j > i; j--) {
                //display/add star
                System.out.print("*");
            }
            // add new line
            System.out.println("");
        }
    }
 

}



Output :


No comments:

Post a Comment