Java Program – Pattern 1

For n = 5

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

Program

public class Pattern {

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

Given input value n, this pattern has n lines and each line contains n asterisks. We use a nested loop. The outer loop counter i is used to count the line number. Since there are n lines, the counter i counts from 1 to n. The inner loop counter j counts the number of asterisks being printed on line i. Since each line has n asterisks, the counter j runs from 1 to n.

One thought on “Java Program – Pattern 1

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>