void main()
{
int n;
// int i=0,j,k=1,a[15][15];
//clrscr();
printf("n Enter any odd number>1 t");
scanf("%d",&n);
if(n%2==0)
{
printf("This program works only for odd numbersn");
}
else
{
int i=0,j,k=1,a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=0;
}
}
i=0;
j=n/2;
a[i][j]=k;
while(k<(n*n))
{
i=i-1;
j=j+1;
if((i<0)&&(j>=n))
{
j=j-1;
i=i+2;
}
else if(i<0)
{
i=n-1;
}
else if(j>=n)
{
j=0;
}
// else
// {
if(a[i][j]!=0)
{
j=j-1;
i=i+2;
}
k=k+1;
a[i][j]=k;
// }
}
printf("n the magic squares for given order isn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%dt",a[i][j]);
}
printf("n");
}
}
getch();
}
Responses
0 Respones to "C program for odd order magic squares generalized"
Post a Comment