Thursday 16 April 2015

magic square

#include<stdio.h>
#include<conio.h>
#define row 3
#define col 3
void main()
{
int ms[row][col]={0},n=1,cr,cc,pr,pc;
cr=0,cc=col/2;
ms[cr][cc]=n++;
{
pr=cr;
cr--;
if(cr<0)
{

cr=row-1;
pc=cc;
cc++;
}
if(cr>2)
{
cc=0;
}
if(ms[cr][cc]!=0)
{
cr=pr=1;
cc=pc;
}
ms[cr][cc]=n++;

}
clrscr();
printf("\n \n magic square:\n");
for(cr=0;cc<row;cr++)
{
for(cc=0;cc<col;cc++)
{
printf("\n %3d",ms[cr][cc]);
}
printf("\n\n");
}
getch();
}

No comments:

Post a Comment