-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternPractice.java
More file actions
220 lines (201 loc) · 4.26 KB
/
PatternPractice.java
File metadata and controls
220 lines (201 loc) · 4.26 KB
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
//Q1
//INPUT : 5
//OUTPUT
/*
12345
23451
34512
45123
51234
*/
//Q2
//INPUT : 5
//OUTPUT
/*
1
21
321
4321
54321
*/
//Q3
//INPUT : 5
//OUTPUT
/*
1
2 4
3 5 7
6 8 10 12
9 11 13 15 17
*/
//Q4
//INPUT : 5
//OUTPUT
/*
555555555
544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
*/
//Q5
//INPUT : 5
//OUTPUT
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
import java.lang.*;
import java.util.*;
public class PatternPractice
{
static int i , j;
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number : ");
int num = scanner.nextInt();
scanner.close();
Q1(num);
Q2(num);
Q3(num);
Q4(num);
Q5(num);
}
public static void Q1(int num)
{
System.out.println("Q1 \n");
for (i = 1; i < num+1; i++)
{
for(j=i; j < num+1; j++)
{
System.out.print(j +" ");
}
for(j=1; j < i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("");
}
public static void Q2(int num)
{
System.out.println("Q2 \n");
for(i = 0 ; i < num ; i++)
{
for(j=0 ; j < num-i-1 ; j++)
{
System.out.print(" ");
}
for(j=i+1 ; j > 0; j--)
{
System.out.print(j+ " ");
}
System.out.println();
}
System.out.println("");
}
public static void Q3(int num)
{
System.out.println("Q3 \n");
int even =2 , odd=1;
for(i = 1; i <=num; i++)
{
for(j=1; j<num-i+1 ;j++)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
if(i%2==0)
{
System.out.print(even+" ");
even+=2;
}
else
{
System.out.print(odd+" ");
odd+=2;
}
}
System.out.println();
}
System.out.println("");
}
public static void Q4(int num)
{
System.out.println("Q4 \n");
int len=0;
for(int counter = 1 ;counter < num+1 ; counter++)
{
len++;
}
len=(2*len)-1;
int[][] pattern = new int[len][len];
i = 0;
j = 0;
for(int counter = num ; counter > 0 ; counter--)
{
int k,l;
for(k=i;k<len;k++)
{
for(l=j;l<len;l++)
{
pattern[k][l] = counter;
if(k+l>len-1)
{
pattern[k][l] = pattern[len-1-k][len-1-l];
}
}
}
i++;
j++;
}
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
System.out.print(pattern[i][j]+" ");
}
System.out.println();
}
System.out.println("");
}
public static void Q5(int num)//pascal triangle
{
System.out.println("Q5 \n");
PatternPractice ObjectForFunction = new PatternPractice();
for(i=0;i<num;i++)
{
for(j=0;j<num-i-1;j++)
{
System.out.print(" ");
}
for(j=0;j<=i;j++)
{
System.out.print(ObjectForFunction.factorial(i)/(ObjectForFunction.factorial(j)*ObjectForFunction.factorial(i-j)) + " ");// n!/(j-r)!*r!
}
System.out.println();
}
System.out.println("");
}
public int factorial(int x)
{
if(x==0)
{
return 1;
}
else
{
return x * factorial(x-1);
}
}
}