Java Program – Pattern 3

For n = 5
1*****
2****
3***
4**
5*
Program
1public class Pattern {
2 
3    public void print(int n) {
4        for (int i = n; i >= 1; i--) {
5            for (int j = 1; j <= i; j++) {
6                System.out.print("*");
7            }
8            System.out.println();
9        }
10    }
11}
Given the value n, this pattern contains n lines. Instead of numbering the lines as 1, 2, 3 … n, we number them as n, n-1, n-2, … , 3, 2, 1. When we number the lines in this way, line i contains i asterisks.

No comments:

Post a Comment