This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
PatternQuestions.java
390 lines (354 loc) · 9.69 KB
/
PatternQuestions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package com.Abhi;
public class PatternQuestions {
public static void main(String[] args) {
// pattern5(7);
// pattern6(5);
// pattern17(4);
// pattern28(5);
// pattern30(5);
// pattern31(5);
// pattern7(5);
// pattern8(5);
// pattern9(5);
// pattern10(5);
// pattern11(6);
// pattern12(5);
// pattern13(5);
// pattern14(5);
// pattern15(5);
}
public static void pattern6(int n) {
/*
*
**
***
****
*****
*/
for (int i = 0; i <n ; i++) {
for(int j=0;j<n-1-i;j++){
System.out.print(" ");
}
for (int stars = 0; stars <=i ; stars++) {
System.out.print("*");
}
System.out.println();
}
}
public static void pattern5(int n){
/*
*
**
***
****
*****
****
***
**
*
*/
for(int i=0;i<2*n-1;i++){
if(i<n){
for(int j=0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}else{
for(int j=0;j<2*n-i-1;j++){
System.out.print("*");
}
System.out.println();
}
}
}
public static void pattern28(int n){
/*
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
*/
for (int i = 1; i <2*n; i++) {
int numberOfCols= i<=n ? i: 2*n-i; //This statement means if i<=n then numberCols=i else it is equal to 2n-i;
int numberOfSpaces= n-numberOfCols;
for (int spaces = 1; spaces <= numberOfSpaces; spaces++) {
System.out.print(" ");
}
for (int cols = 1; cols <= numberOfCols ; cols++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void pattern30(int n){
/*
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
*/
for(int i=1;i<=n;i++){
int numberOfSpaces= n-i;
for (int spaces = 1; spaces <= numberOfSpaces ; spaces++) {
System.out.print(" ");
}
for (int cols1 = i; cols1 >1 ; cols1--) {
System.out.print(cols1+" ");
}
for (int cols2 = 1; cols2 <= i; cols2++) {
System.out.print(cols2+" ");
}
System.out.println();
}
}
public static void pattern17(int n){
/*
1
212
32123
4321234
32123
212
1
*/
for (int row = 1; row <2*n ; row++) {
int numberOfSpaces= row<=n ? n-row: row-n;
for (int spaces = 1; spaces <=numberOfSpaces; spaces++) {
System.out.print(" ");
}
int colValue= row<=n ? row:2*n-row;
for (int cols1 = colValue; cols1 >=1; cols1--) {
System.out.print(cols1+" ");
}
for (int cols2 = 2; cols2 <=colValue ; cols2++) {
System.out.print(cols2+" ");
}
System.out.println();
}
}
public static void pattern31(int n){
/*
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
*/
//This figure is a square.
int last=2*n-1;
for (int row = 0; row < last; row++) {
for (int col = 0; col < last; col++) {
int indexValue= Math.min(Math.min(row,col),Math.min((last-1-col),(last-1-row)));
System.out.print(n-indexValue+" ");
}
System.out.println();
}
}
public static void pattern7(int n){
/*
*****
****
***
**
*
*/
for (int i = 0; i < n; i++) {
for (int stars = 0; stars < n-i; stars++) {
System.out.print("*");
}
System.out.println();
}
}
public static void pattern8(int n){
/*
*
***
*****
*******
*********
*/
for (int rows = 0; rows < n; rows++) {
for (int spaces = 0; spaces < n-rows-1; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < 2*rows+1; stars++) {
System.out.print("*");
}
System.out.println();
}
}
public static void pattern9(int n){
/*
*********
*******
*****
***
*
*/
for (int rows = 0; rows < n; rows++) {
for (int spaces = 0; spaces <= rows; spaces++) {
System.out.print(" ");
}
for (int stars = 2*rows+1; stars <2*n; stars++) {
System.out.print("*");
}
System.out.println();
}
}
public static void pattern10(int n){
/*
*
* *
* * *
* * * *
* * * * *
*/
for (int row = 0; row < n; row++) {
for (int spaces = 0; spaces < n-1-row; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < row+1; stars++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void pattern11(int n){
/*
* * * * *
* * * *
* * *
* *
*
*/
for (int rows = 0; rows < n; rows++) {
for (int spaces = 0; spaces < rows; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < n-rows; stars++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void pattern12(int n){
/*
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
*/
for (int row = 0; row < 2*n; row++) {
int reqSpaces= row<n ? row: 2*n-row-1;
for (int spaces = 0; spaces < reqSpaces; spaces++) {
System.out.print(" ");
}
int reqStars= row<n? n-row: row-n+1;
for (int stars = 0; stars < reqStars ; stars++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void pattern13(int n){
/*
*
* *
* *
* *
*********
*/
for (int row = 1; row <= n; row++) {
//spaces
for (int spaces = 1; spaces <= n-row; spaces++) {
System.out.print(" ");
}
//stars and spaces.
for (int i = 1; i <= 2*row-1; i++) {
if(row==n || i==1 || i==2*row-1 ){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
public static void pattern14(int n){
/*
*********
* *
* *
* *
*
*/
for (int row = 1; row <=n ; row++) {
//spaces
for (int spaces = 1; spaces < row; spaces++) {
System.out.print(" ");
}//hollow part and the stars.
int endPoint= 2*n - (2*row-1);
for (int i = 1; i <=endPoint ; i++) {
if(row==1 || i==1 || i==endPoint){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
public static void pattern15(int n){
/*
*
* *
* *
* *
* *
* *
* *
* *
*
*/
for (int row = 1; row <2*n ; row++) {
int reqSpaces= row<=n? n-row: row-n;
//Equation for counting the number of spaces.
for (int spaces = 0; spaces < reqSpaces; spaces++) {
System.out.print(" ");
}
//equation for first and last star.
int endPoint= row<=n? 2*row: 2*n - (2*(row-n));
for (int i = 1; i < endPoint; i++) {
if(i==1 || i==endPoint-1){ //Could have done the -1 part while making the equation as well but this looked more clean to me :)
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
public static void pattern16(int n){
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
}
}