#include<conio.h>
#include<stdio.h>
#define max 25
int square[max][max];
int check(int);
int check(int n)
{
int column,row,k,sum,tsum=0;
sum=(int)(( n * ( n * n + 1)))/2;
/* checking sum of each row is same */
printf("\nSum of rows: ");
for(row=0;row<n;row++)
{
tsum=0;
for(column=0;column<n;column++)
{
tsum=tsum+square[column][row];
}
if(tsum!=sum)
return 0;
}
/* checking sum of each coloumn is same */
printf("\nSum of columns: ");
printf("\nSum of diagonals: ");
for(column=0;column<n;column++)
{
tsum=0;
for(row=0;row<n;row++)
{
tsum=tsum+square[column][row];
}
if(tsum!=sum)
return 0;
}
/* checking sum of diagonal is same */
for(column=0;column<n;column++)
{
tsum=0;
for(row=n-1;row>=0;row--)
{
tsum=tsum+square[column][row];
}
if(tsum!=sum)
return 0;
}
return 1;
/* checking sum of diagonal is same */
tsum=0;
for(column=0;column<n;column++)
{
tsum=tsum+square[column][column];
}
if(tsum!=sum)
return 0;
}
void main()
{
int column,row,k,size,flag;
//clrscr();
printf("\nPlease input the size of the square: ");
scanf("%d",&size);
printf("\nPlease input the square row by row: \n",size * size);
for(column=0;column<size;column++)
{
for(row=0;row<size;row++)
{
scanf("%d",&square[column][row]);
}
}
flag=check(size);
if(flag)
{
printf("\n /-------------------------\\ ");
printf("\n | This is a magic square. |");
printf("\n \\-------------------------/ ");
}
else
{
printf("\n /-----------------------------\\ \n");
printf("\n | This is NOT a magic square. | \n");
printf("\n \\-----------------------------/ \n ");
}
}