forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic_square.cpp
58 lines (38 loc) · 825 Bytes
/
magic_square.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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;
}