-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpiral_print.java
62 lines (62 loc) · 1.41 KB
/
Spiral_print.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
package Programs2;
import java.util.*;
public class Spiral_print {
public static void main(String[] args) {
System.out.print("Enter the number of rows in the array : ");
Scanner s=new Scanner(System.in);
int size1=s.nextInt();
System.out.print("Enter the number of columns in the array : ");
int size2=s.nextInt();
int arr[][]=new int[size1][size2];
input(arr,size1,size2);
System.out.println();
System.out.print("Array in Spiral form is : ");
spiralPrint(arr);
}
public static void input(int arr[][],int size1,int size2)
{
Scanner s=new Scanner(System.in);
int ele=1;
for(int i=0;i<size1;i++)
{
for(int j=0;j<size2;j++)
{
System.out.print("Enter the element "+ ele++ +" of the array : ");
arr[i][j]=s.nextInt();
}
}
System.out.print("Array formed is : ");
display(arr,size1,size2);
}
public static void display(int[][] arr, int size1,int size2) {
for(int i=0;i<size1;i++)
for(int j=0;j<size2;j++)
System.out.print(arr[i][j]+" ");
}
public static void spiralPrint(int arr[][]){
int k=0,i,j,l=0;
int n=arr.length;
int m=arr[0].length;
while(k<n&&l<m)
{
for(i=l;i<m;++i)
System.out.print(arr[k][i]+" ");
k++;
for(i=k;i<n;++i)
System.out.print(arr[i][m-1]+" ");
m--;
if(k<n)
{
for(i=m-1;i>=l;--i)
System.out.print(arr[n-1][i]+" ");
n--;
}
if(l<m)
{
for(i=n-1;i>=k;--i)
System.out.print(arr[i][l]+" ");
l++;
}
}
}
}