-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral_Input.java
More file actions
36 lines (34 loc) · 876 Bytes
/
Spiral_Input.java
File metadata and controls
36 lines (34 loc) · 876 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
import java.util.*;
public class Spiral_Input
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int x = sc.nextInt();
int a[][]=new int[x][x];
int i,j,r=0,c=0,r1=x-1,c1=x-1,n=x*x;
while(n>=1)
{
for(i=c;i<=c1;i++)
a[r][i]=n--;
for(j=r+1;j<=r1;j++)
a[j][c1]=n--;
for(i=c1-1;i>=c;i--)
a[r1][i]=n--;
for(j=r1-1;j>=r+1;j--)
a[j][c]=n--;
r++;
c++;
r1--;
c1--;
}
System.out.println("The Array :");
for (i=0;i<x;i++)
{
for(j=0;j<x;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}