-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral matrix printing.c
52 lines (47 loc) · 1.13 KB
/
spiral matrix printing.c
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
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
int main()
{
int i,j,n;
scanf("%d\n",&n);
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
int k,l=0,r=n-1,u=0,d=n-1;
int dir=0;
while(l<=r && u<=d)
{
if(dir==0){
for(k=l;k<=r;k++)
printf("%d",a[l][k]);
u++;
}
else if(dir==1){
for(k=u;k<=d;k++)
printf("%d",a[k][r]);
r--;
}
else if(dir==2){
for(k=r;k>=l;k--)
printf("%d",a[d][k]);
d--;
}
else if(dir==3){
for(k=d;k>=u;k--)
printf("%d",a[k][l]);
l++;
}
dir=(dir+1)%4;
}
return 0;
}