-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpiral_matrix_by_Debdut.java
91 lines (90 loc) · 2.12 KB
/
Spiral_matrix_by_Debdut.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
/**
* Write a description of class Spiral_matrix here.
*
* @author (Debdut Goswami)
* @version (3.1.5)
*/
import java.util.*;
public class Spiral_matrix_by_Debdut
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the order of the matrix?");
int r=sc.nextInt();
int c=sc.nextInt();
int arr[][]=new int[r][c];
int i=1,j=0,k=-1,l=0,m=0,n=0,x=r*c;
while(i<=x)
{
if(l==0)
{
for(int a=0;;)
{
arr[j][++k]=i++;
if(k==c-1)
{
l+=1;
break;
}
}
}
else if(l==1)
{
for(int a=0;;)
{
arr[++j][k]=i++;
if(j==r-1)
{
l+=1;
break;
}
}
}
else if(l==2)
{
for(int a=0;;)
{
arr[j][--k]=i++;
if(k==n)
{
l+=1;
break;
}
}
}
else if(l==3)
{
for(int a=0;;)
{
if(arr[j-1][k]>0)
{
l+=1;
break;
}
arr[--j][k]=i++;
if(j==n)
{
l+=1;
break;
}
}
}
if(l>3)
{
l=0;
r--;
c--;
n+=1;
}
}
for(i=0;i<arr.length;i++)
{
for(j=0;j<arr[i].length;j++)
{
System.out.printf("%5d",arr[i][j]);
}
System.out.println();
}
}
}