Algorithm
- Initialize a matrix as matr[][] along with declaring variables
- a return type function to return the result at the end utilizing function calls
- Declare variable sum and calculate the sum of prime diagonal
- use of for loop for finding some of the rows
- use of for loop for finding some of the columns
- if-else statement for checking the sum of both rows and columns is the same as that of diagonals
- primary function with input matrix values for checking whether it's a magic square or not
Program
Program codes for checking whether a matrix is a square matrix or not using the algorithm mentioned above.
C++ Code
//C++ code in order to check for magic square for a matrix
#include <bits/stdc++.h>
using namespace std;
// it will return true if the matr[][] is a magic
//square otherwise it will return false
bool isMagicSquare(int matr[][N])
{
//find the prime diagonal sum of the matrix
int sum = 0;
for (int i = 0; i < N; i++)
sum = sum + matr[i][i];
// sums of the rows in the matrix
for (int i = 0; i < N; i++) {
int rowSum = 0;
for (int j = 0; j < N; j++)
rowSum += matr[i][j];
// condition for each row sum to be same as sum of prime diagonal
if (rowSum != sum)
return false;
}
// For sums of Columns
for (int i = 0; i < N; i++) {
int colSum = 0;
for (int j = 0; j < N; j++)
colSum += matr[j][i];
// condition for each row sum to be the same as the sum of prime diagonal
if (sum != colSum)
return false;
}
return true;
}
// program to check above function
int main()
{
int mat[][N] = {{ 3, 8, 7 },
{ 10, 6, 2 },
{ 5, 4, 9 }};
if (isMagicSquare(mat))
cout << "It’s a Magic Square";
else
cout << "It’s not a Magic Square";
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

Java Code
import java.io.*;
class CN {
static int N = 3;
static boolean isMagicSquare(int mat[][])
{
int sumd1 = 0,sumd2=0;
for (int i = 0; i < N; i++)
{
sumd1 += mat[i][i];
sumd2 += mat[i][N-1-i];
}
//if the two diagonal sums are not equal then it isn't a magic square
if(sumd1!=sumd2)
return false;
for (int i = 0; i < N; i++) {
int rowSum = 0, colSum = 0;
for (int j = 0; j < N; j++)
{
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
return false;
}
return true;
}
// driver program
public static void main(String[] args)
{
int mat[][] = {{ 3, 8, 7 },
{ 10, 6, 2 },
{ 5, 4, 9 }};
if (isMagicSquare(mat))
System.out.println("Its aMagic Square");
else
System.out.println("Its not a Magic Square");
}
}

You can also try this code with Online Java Compiler
Run Code
Output :

Python Code
def isMagicSquare( mat) :
n = len(mat)
sumd1=0
sumd2=0
for i in range(n):
sumd1+=mat[i][i]
sumd2+=mat[i][n-i-1]
# if the two diagonal sums are not equal then it isn't a magic square
if not(sumd1==sumd2):
return False
for i in range(n):
# rowsum and column sum
sumr=0
sumc=0
for j in range(n):
sumr+=mat[i][j]
sumc+=mat[j][i]
if not(sumr==sumc==sumd1):
return False
return True
# Driver Code
mat = [ [ 3, 8, 7 ],
[ 10, 6, 2],
[ 5, 4, 9 ] ]
if (isMagicSquare(mat)) :
print( "Its a Magic Square")
else :
print( "Its not a magic Square")

You can also try this code with Online Python Compiler
Run Code
Output

Also see, Rabin Karp Algorithm
Frequently Asked Questions
How many different kinds of magic squares are there?
There is just one magic square. The eight patterns are rotations and reflections that match the square's symmetries.
What makes magic squares unique?
Magic Squares are square grids that have a unique numerical layout. These numbers are unique because they add up to the same number in every row, column, and diagonal.
Can you make a magic square repeat number?
Many magic squares would become trivially easy if you could repeat integers, such as a grid comprised entirely of 1s that tallied up to 3! That isn't really remarkable.
Is it possible to have an unlimited number of magic squares?
A magic square is an n×n grid of integers in which the sum of the number in each row, column, and diagonal equals a magic constant M. This process produces an endless magic square with zero sums in each row, column, and diagonal.
Conclusion
In this blog, we have discussed whether the given matrix is a magic square or not. We introduced the magic square, its formula, and algorithm and implemented it in different languages.
We hope this blog has improved your understanding of the odd-order magic square.
Click here to learn about Data Structure here.
To learn more and become more adept read our blogs on Operations on Matrices, Construct a Linked List from a 2D Matrix, Book Allocation Problem, Matrix Chain Multiplication, and Types of Matrix for additional information and must visit our practice site Coding Ninjas Studio and practice top problems, take Mock Tests, practice extra SQL questions here, and read educational articles and interview experiences. Please vote for our blog so that other ninjas can benefit from it.
Happy Learning!