-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPattern
More file actions
44 lines (35 loc) · 890 Bytes
/
Pattern
File metadata and controls
44 lines (35 loc) · 890 Bytes
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
Pattern for N=5
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
code in java **************************************
public class Solution {
public static void printPattern(int n){
int p = n;
if(n >= 1 && n <= 100){
for(int i = 1; i <=n; i+=2){
int k = (i - 1) * n + 1;
for(int j = 0; j < n-1; j++){
System.out.print(k + " ");
k++;
}
System.out.println(k + " ");
}
}
if(n % 2 != 0){
p = n - 1;
}
for(int i=p;i>0;i-=2)
{
int k=(i-1)*n+1;
for(int j=0;j<n-1;j++)
{
System.out.print(k + " ");
k++;
}
System.out.println(k + " ");
}
}
}