Sunday, December 27, 2009

Magic box

My first post ... small C program written on borland C to make magic box ...!
(Enter the size then it will create the magic box as the sum of any of its columns,rows and diagonal will be the same )


#include
#include
#include
void main(void)
{
int col=0,row=0,size=0,counter=1;
clrscr();
printf("Enter the size of the matrix: ");
scanf("%d",&size);
col =(size + 1) / 2;
row = 1;
gotoxy(col*3,row*3);
printf("%d",counter);
while (counter < size * size )
{
if (counter % size != 0)
{
col --;
row --;
if (col == 0) col = size;
if (row == 0) row = size;
gotoxy(col*3,row*3);
counter ++;
printf("%d",counter);


}
else
{
row ++;
if (row > size ) row = 1;
gotoxy(col*3,row*3);
counter ++;
printf("%d",counter);

}


}
getch();
}

No comments:

Post a Comment