Java Program – Pattern 5

For n = 7

* * * *
 * * * 
* * * *
 * * * 
* * * *
 * * * 
* * * *

Program

public class Pattern {

    public void print(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if ((i + j) % 2 == 0) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
       
    }
}

Given n, the pattern contains n lines. The pattern contains asterisks and spaces alternately. The sum of asterisks and spaces in each line is equal to n. The character at row i and column j is an asterisk if i+j is an even numbner. Else, it is a space.

7 thoughts on “Java Program – Pattern 5

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>