2
2
3
3
public class Pattern1 {
4
4
public static void main (String [] args ) {
5
- pattern2 (5 );
5
+ pattern5 (5 );
6
6
}
7
7
8
8
// pattern Output
@@ -35,7 +35,7 @@ static void pattern1(int n) {
35
35
}
36
36
37
37
// method 2
38
- static void pattern2 (int n ) {
38
+ static void pattern1_1 (int n ) {
39
39
for (int i = 0 ; i < 2 * n + 1 ; i ++) {
40
40
int colInRow = (i > n ) ? 2 * n - i : i ;
41
41
for (int j = 0 ; j <= colInRow ; j ++) {
@@ -44,4 +44,103 @@ static void pattern2(int n) {
44
44
System .out .println ();
45
45
}
46
46
}
47
+
48
+ static void pattern2 (int n ){
49
+ for (int i = 0 ; i < n *2 +1 ; i ++) {
50
+ int colInRow = (i > n ) ? 2 * n - i : i ;
51
+ for (int j = colInRow ; j < n ; j ++) {
52
+ System .out .print (" " );
53
+ }
54
+ for (int j = 0 ; j <= colInRow ; j ++) {
55
+ System .out .print ("*" );
56
+ }
57
+ System .out .println ();
58
+ }
59
+ }
60
+ // pattern Output
61
+ // *
62
+ // **
63
+ // ***
64
+ // ****
65
+ // *****
66
+ // ******
67
+ // *****
68
+ // ****
69
+ // ***
70
+ // **
71
+ // *
72
+ static void pattern3 (int n ){
73
+ for (int i = 0 ; i < n *2 +1 ; i ++) {
74
+ int colInRow = (i > n ) ? 2 * n - i : i ;
75
+ for (int j = colInRow ; j < n ; j ++) {
76
+ System .out .print (" " );
77
+ }
78
+ for (int j = 0 ; j <= colInRow ; j ++) {
79
+ System .out .print ("* " );
80
+ }
81
+ System .out .println ();
82
+ }
83
+ }
84
+ // Pattern output
85
+ // *
86
+ // * *
87
+ // * * *
88
+ // * * * *
89
+ // * * * * *
90
+ // * * * * * *
91
+ // * * * * *
92
+ // * * * *
93
+ // * * *
94
+ // * *
95
+ // *
96
+
97
+ static void pattern4 (int n ){
98
+ for (int i = 0 ; i < n *2 +1 ; i ++) {
99
+ int colInRow = (i > n ) ? 2 * n - i : i ;
100
+ for (int j = 0 ; j <= colInRow ; j ++) {
101
+ System .out .print ('*' );
102
+ }
103
+ for (int j = colInRow ; j < n ; j ++) {
104
+ System .out .print (" " );
105
+ }
106
+ for (int j = colInRow ; j < n ; j ++) {
107
+ System .out .print (" " );
108
+ }
109
+ for (int j = 0 ; j <= colInRow ; j ++) {
110
+ System .out .print ("*" );
111
+ }
112
+ System .out .println ();
113
+ }
114
+ }
115
+ // pattern output
116
+ // * *
117
+ // ** **
118
+ // *** ***
119
+ // **** ****
120
+ // ***** *****
121
+ // ************
122
+ // ***** *****
123
+ // **** ****
124
+ // *** ***
125
+ // ** **
126
+ // * *
127
+
128
+ static void pattern5 (int n ){
129
+ for (int i = 0 ; i < n *2 +1 ; i ++) {
130
+ int colInRow = (i > n ) ? 2 * n - i : i ;
131
+ for (int j = colInRow ; j <= n ; j ++) {
132
+ System .out .print ("*" );
133
+ }
134
+ for (int j = 0 ; j < colInRow ; j ++) {
135
+ System .out .print (" " );
136
+ }
137
+ for (int j = 0 ; j < colInRow ; j ++) {
138
+ System .out .print (" " );
139
+ }
140
+ for (int j = colInRow ; j <= n ; j ++) {
141
+ System .out .print ("*" );
142
+ }
143
+ System .out .println ();
144
+ }
145
+ }
47
146
}
0 commit comments