Java Program – Pattern 2

For n = 5

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

Program

public class Pattern {

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

Given n, the pattern contains n lines. And line i contains i asterisks i.e. first line contains one asterisk, second line contains two asterisks and so on. The loop counter i counts the line number which ranges from 1 to n. The inner loop counter counts from 1 to i, since line i has i asterisks.

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>