C Program to check matrix is magic square or not

Write a c program to check whether the given matrix is a magic square matrix or not.

A matrix is magic square matrix, if all rows sum, columns sum and diagonals sum are equal.

Example:
8   1   6
3   5   7
4   9   2

is the magic square matrix. As you can see numbers in first row add up to 15 (8 + 1 + 6), so do the numbers of 2nd row 3 + 5 + 7. Also the number in the last row 4 + 9 + 2. Similarly, the columns all add up to the same number 15. Hence, this matrix is a magic square matrix.

The Program

#include <stdio.h>
void main() {
int A[50][50];
int i, j, M, N;
int size;
int rowsum, columnsum, diagonalsum;
int magic = 0;
printf("Enter the order of the matrix:\n");
scanf("%d %d", &M, &N);
if(M==N) {
printf("Enter the elements of matrix \n");
for(i=0; i<M; i++) {
for(j=0; j<N; j++) {
scanf("%d", &A[i][j]);
}
}
printf("\n\nMATRIX is\n");
for(i=0; i<M; i++) {
for(j=0; j<N; j++) {
printf("%3d\t", A[i][j]);
}
printf("\n");
}
// calculate diagonal sum
diagonalsum = 0;
for(i=0; i<M; i++) {
for(j=0; j<N; j++) {
if(i==j) {
diagonalsum = diagonalsum + A[i][j];
}
}
}
// calculate row sum
for(i=0; i<M; i++) {
rowsum = 0;
for(j=0; j<N; j++) {
rowsum = rowsum + A[i][j];
}
if(rowsum != diagonalsum) {
printf("\nGiven matrix is not a magic square");
return;
}
}
// calculate column sum
for(i=0; i<M; i++) {
columnsum = 0;
for(j=0; j<N; j++) {
columnsum = columnsum + A[j][i];
}
if(columnsum != diagonalsum) {
printf("\nGiven matrix is not a magic square");
return;
}
}
printf("\nGiven matrix is a magic square matrix");
} else {
printf("\n\nPlease enter the square matrix order(m=n) \n\n");
}
}
view raw magic_square.c hosted with ❤ by GitHub

Sample Output

Read more about C Programming Language and read the C Programming Language (2nd Edition). by K and R.

Related Programs

To get regular updates on new C programs, you can Follow @c_program
You can discuss these programs on our Facebook Page. Start a discussion right now,

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

(c) www.c-program-example.com

6 comments on “C Program to check matrix is magic square or not

    • Sandeepa Nadahalli says:

      Hi Shweta,

      Adding flowchart to the blog post may take a while as I am working on post by post. Please get in touch over email or Facebook and I will provide the flow chart. Also, I will be happy to explain how this program works.

      Happy coding!

      Reply
  • I am SAP ABAP developer looking for magic square matrix. Search engine landed me to your page.

    Having done some C during college days, I was able to understand. I could apply the concept in ABAP language and get my magic square in ABAP. 🙂

    Thanks for this post.

    Raju

    Reply
    • Sandeepa Nadahalli says:

      Hi Raju,

      I’m very happy this blog post helped you.

      Thanks for your comment. That encourages me to write more.

      Happy coding!

      Cheers,
      Sandeepa

      Reply
  • Hishe Hintsa Fisseha says:

    Dear Sandeepa,

    Just wanted to suggest a little modification on your program.

    In its present form, your code does not check if the sums of the rows(sum1) are equal to sum. Eg. try this one

    2 6 3
    5 4 3
    6 3 7

    or

    4 8 8
    7 5 1
    4 2 6

    Your program will tell that these matrices are magic matrices, Even though the fact is that they aren’t b/c the sums of the rows is different.

    Reason for why this is happening: You used the variable flag to check for the sums of the rows and then again to check for the sums of the column. By doing so you are overwriting the former value of flag by the latter.

    Suggested Solution1: use two different flags for the two loops(may be flag1 and flag2) and check if both of them are equal to 1.

    Suggested Solution2: use print result and return instead of break inside the two for loops. (I would prefer this one).

    Thank you!!!

    PS: \n and \t are written as n and t. I hope you can edit them as well.

    Reply
    • Sandeepa Nadahalli says:

      Hello Hishe Hintsa Fisseha,

      Thank you very much for your comment. I appreciate you spending time to write such detailed feedback.

      I have modified the program now. Do check when you get time.

      I have lost some \n s when I moved my blog from blogger to WordPress!

      Reply

Leave a Reply