(using c)Generating Magic Squares of odd order using De la Loubre's method

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-#

2ng4vid
#include
#include
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();
}

Replies

You are reading an archived discussion.

Related Posts

May i know what is a control word in a small program in 8051? for example, WHAT IS A CONTROL WORD IN ADDING TO 8 BIT NUMBERS?
Its good to be among people with like minds. The Engineers.
President Obama and his team have taken steps to ensure that the top engineering talent remains in USA and does not move back to their hometowns. The USA Startup aims...
evoMouse is a tiny bluetooth, USB enabled devices that users infrared sensors to create a virtual mouse or full QWERTY keyboard on a flat surface. Check out an interesting video...
I've been receiving lot of internship related queries from Mechanical Engineering students via email. I'm not at all aware of any companies that offer summer internships or trainings to mechanical...