Java Program – Pattern 2

For n = 5
1*
2**
3***
4****
5*****
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 <= i; j++) {
6                System.out.print("*");
7            }
8            System.out.println();
9        }
10    }
11}
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.

No comments:

Post a Comment