Monday 15 February 2016

Java program which prints the pattern

Write a Java program which prints the following pattern on the console when the input for no. of rows (n=5).


A.

public class TestPattern {
    public static void main(String[] args) {
        int rows=5;
        print(rows);
    }
    static void print(int rowCount){       
        for(int i=1;i<=rowCount;i++){
           
            boolean isEvenRow = i%2==0;
            if(isEvenRow){
                System.out.print("  ");
            }
           
            int columnCount = isEvenRow?(rowCount-2):rowCount;
           
            for(int j=1;j<=columnCount;j++){
                System.out.print("* ");               
            }
           
            System.out.println();
        }
    }
   

}

No comments:

Post a Comment