Q) Write a program to print spiral matrix .
Sol)
#include<stdio.h>
#include<conio.h>
void main()
{
int m[20][20],i,j;
int lc,hc,lr,hr,r,c,cnt ;
clrscr();
printf("www.c-programcodes.co.in");
printf("\n Enter r & c ::") ;
printf("\n Enter r & c ::") ;
scanf("%d%d" ,&r, &c) ;;
cnt=1;
lr=0, lc =0 ;
hr= r-1;
hc = c-1;
while(lr<=hr && lc <= hc )
{
i = lr;
for(j=lc;j<= hc ; j++)
{
m[i][j] = cnt++ ;
j= hc;
for(i=lr+1;i<=hr)
{
m[i][j]= cnt++;
if( lr != hr)
{
i =hr;
for(j=hc-1;j>=lc;j--)
m[i][j] = cnt++ ;
}
if(lc != hc)
{
j =hc;
for(i=hr-1;i>lr;i--)
m[i][j] = cnt++ ;
}
}
}
lr++;
lc++;
hr--;
hc--;
}
printf("\n Spirally filled matrix is \n");
for(i=0;i<r ; i++)
{
for(j=0;j<c;j++)
{
printf("%4d", m[i][j]) ;
printf("\n") ;
}
}
}