#include<stdio.h>
main()
{
int n=0,i=0,j,k=0,a[9][9];
printf("\n Enter N : ");
scanf("%d",&n);
if(n%2==0)
{
printf("\n Cannot create magic matrix for even number !");
exit(0);
}
j=n/2;
for(k=1;k<=n*n;k++)
{
a[i][j]=k;
i--;
j++;
if(k%n==0)
{
i=i+2;
j--;
}
if(j==n)
{
j=j-n;
}
if(i<0)
{
i=i+n;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n");
}
getch();
}
main()
{
int n=0,i=0,j,k=0,a[9][9];
printf("\n Enter N : ");
scanf("%d",&n);
if(n%2==0)
{
printf("\n Cannot create magic matrix for even number !");
exit(0);
}
j=n/2;
for(k=1;k<=n*n;k++)
{
a[i][j]=k;
i--;
j++;
if(k%n==0)
{
i=i+2;
j--;
}
if(j==n)
{
j=j-n;
}
if(i<0)
{
i=i+n;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t %d",a[i][j]);
}
printf("\n");
}
getch();
}
No comments:
Post a Comment