-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspiral_inward.java
More file actions
49 lines (43 loc) · 919 Bytes
/
spiral_inward.java
File metadata and controls
49 lines (43 loc) · 919 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
45
46
47
48
49
import java.util.*;
class spiral_inward
{
public static void main()
{
Scanner ob=new Scanner(System.in);
int i,j;
System.out.println("enter n");
int n=ob.nextInt();
int a[][]=new int[n][n];
int r=0,c=-1,p;
int x=0;
for(i=0;i<n;i++)
{
p=(int)Math.pow(-1,i);
for(j=0;j<(n-i);j++)
{
c=c+(1*p);
a[r][c]=++x;
}
for(j=0;j<(n-1-i);j++)
{
r=r+(1*p);
a[r][c]=++x;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]<10)
{
System.out.print(a[i][j]+" \t\t");
}
else
{
System.out.print(a[i][j]+"\t\t");
}
}
System.out.println();
}
}
}