(using c)Generating Magic Squares of odd order using De la Loubre's method
@computeridiot007-xUubel
โข
Oct 27, 2024
Oct 27, 2024
1.2K
Magic squares are nothing but where row,column and diagonal sums are constant
for e.g enter odd number say 9 then you will get the output as magic square as follows
Read the method here
#-Link-Snipped-#

#include<stdio.h>
#include<dos.h>
void main()
{
int a[100][100],x,y,tmp=1,n;
clrscr();
printf("\n Enter the odd no for which you want to generate magic square\n");
scanf("%d",&n);
for(x=0;x<=n-1;x++)
{
for(y=0;y<=n-1;y++)
{
a[x][y]=0;
}
}
x=0;
y=(n-1)/2;
if(!(n%2==0))
{
a[x][y]=tmp;
hello:
x-=1;
y+=1;
if(tmp>=n*n)
goto raw;
if((x<0)&&(y>=n))
{
x+=2;
y-=1;
goto another;
}
if(x<0)
x=n-1;
if(y>=n)
y=0;
another:
tmp+=1;
if(a[x][y]==0)
{
a[x][y]=tmp;
}
else
{
y-=1;
x+=2;
a[x][y]=tmp;
}
goto hello;
}
else
{
printf("\n you have not entered an odd number\n");
}
raw:
printf("\n");
for(x=0;x<=n-1;x++)
{
for(y=0;y<=n-1;y++)
{
delay(300);
printf("%d\t",a[x][y]);
}
printf("\n");
}
getch();
}