magic square Algorithm
Beside this, depending on further property, magic squares are also classified as associative magic squares, pandiagonal magic squares, most-perfect magic squares, and so on. Moreover, the term" magic squares" is sometimes also used to refer to various types of word squares. Specimens of magic squares of order 3 to 9 look in an encyclopedia from Baghdad c. 983, the Encyclopedia of the Brethren of Purity (Rasa'il Ikhwan al-Safa).Also notable are the ancient cultures with a tradition of mathematics and numerology that make not observe the magic squares: Greeks, Babylonians, Egyptians, and Pre-Columbian Americans. By the end of 12th century, the general methods for constructing magic squares were well established.
#include <iostream>
#define N 3
using namespace std;
bool isMagicSquare(int mat[][N])
{
int sum = 0;
for (int i = 0; i < N; i++)
sum = sum + mat[i][i];
for (int i = 0; i < N; i++) {
int rowSum = 0;
for (int j = 0; j < N; j++)
rowSum += mat[i][j];
if (rowSum != sum)
return false;
}
for (int i = 0; i < N; i++) {
int colSum = 0;
for (int j = 0; j < N; j++)
colSum += mat[j][i];
if (sum != colSum)
return false;
}
return true;
}
int main()
{
int mat[3][N] ,i,k;
for(i=0; i<3; i++)
{
for(k=0; k<3; k++)
cin>>mat[i][k];
}
if (isMagicSquare(mat))
cout << "Magic Square";
else
cout << "Not a magic Square";
return 0;
}