Java Program – Pattern 5

For n = 7
1* * * *
2 * * *
3* * * *
4 * * *
5* * * *
6 * * *
7* * * *
Program
1public class Pattern {
2 
3    public void print(int n) {
4        for (int i = 1; i <= n; i++) {
5            for (int j = 1; j <= n; j++) {
6                if ((i + j) % 2 == 0) {
7                    System.out.print("*");
8                else {
9                    System.out.print(" ");
10                }
11            }
12            System.out.println();
13        }
14        
15    }
16}
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.

No comments:

Post a Comment