For n = 5
1 | ***** |
2 | ***** |
3 | ***** |
4 | ***** |
5 | ***** |
Program
1 | public 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 | System.out.print( "*" ); |
7 | } |
8 | System.out.println(); |
9 | } |
10 | } |
11 | } |
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.
No comments:
Post a Comment