A C program to check if the given matrix is magic square or not.

By | September 3, 2013

Let’s identify variables needed for this program.

In this program, we need to save matrices which consists Rows and Columns. To save this we need Two dimensional ARRAY.

Multi-dimensional Array is a nothing different than any Array but the only difference is that it has more than one dimension to it e.g. square has two dimension and cube has three dimension. The Dimension of array is decided by us in number of square brackets [] selected. If We select two dimension than we have to take two square brackets[][]. It has to be accessed with the help of index number ranging from 0 to n-1 and 0 to m-1. (e.g. num[n][m] will have num[0][0],num[0][1],num[2][2] so on.)

First few variables will be the one which will save the values of matrix with the choice of rows and columns entered by the user and it will be array A[MAX][MAX] will be in the form A[M][M] therefore we will need M will be needed as user decides the order of the square matrix. Here since we are taking order of the square matrix from the user and hence we can not keep the square brackets empty we will fill that value by maximum value (MAX). This MAX variable will be defined as a constant above the program.Next variable will be array SUM and TEMP will be for saving resultant right diagonal and left diagonal values respectively which will be compared. Next variable will be array SUM_COL[MAX] and SUM_ROW[MAX] will be for saving resultant sum of columns and sum of rows values respectively which will be compared later. other variables will be I and J which will be for FOR Loop so in all EIGHT variables and ONE constant selected.
The identified variables are int A[MAX][MAX],I,J,M,SUM,TEMP,SUM_COL[MAX],SUM_ROW[MAX] and identified constant is MAX.

#define MAX 10

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to M-1. Now here we need nested loop one loop inside the other. So that we can access all the elements with it. We will calculate sum of right diagonal and sum of left diagonal values by using single for loop. We will need First two nested loops for scanning and printing given array values of square matrix. Next two nested loop will be nested with three FOR loop for sum of rows array values and sum of columns array values of matrices respectively and save it empty arrays taken.

This let’s you understand nested loop


MAGIC SQUARE is a square matrix which has same number of rows and same numbers of columns. since the number of row and number of column are same the number is called order of square matrix. When we add the values in any row or any column or right diagonal or left diagonal the sum of all should be the equal number. See the image below to understand.


In the program, first calculate the sum of right diagonal (SUM) and sum of left diagonal (TEMP). Since the right diagonal has all the index values to be (0,0), (1,1),  and (2,2), hence we have only one for loop to execute the summation task.



Next sum of left diagonal (TEMP)  Since the left diagonal has all the index values to be (2,0), (1,1),  and (0,2), hence we have only one for loop to execute the summation task. When we see the index values row decreases and column increases gradually.


To reduce execution time we can jump from the point we find values compared not equal to the end statement and display not a magic square message. If the two diagonal values are not equal (i.e. SUM == TEMP) jump there itself.

Condition for checking if a matrix is a magic square or not : In order to check magic square of a square matrix, sum of each row, sum of each column, and sum of right & left diagonal value, All the sum must be equal. Thus, if A is an m x m matrix, sum 0f right diagonal = sum 0f left diagonal = sum of each row = sum of each column.

       \\code for calculating sum of columns
        \\code to check and compare each value of sum of columns with sum of diagonal and jump to LABEL if not equal
        \\code for calculating sum of rows
        \\code to check and compare each value of sum of rows with sum of diagonal and jump to LABEL if not equal

Calculate sum of each column the variable J should be inside and save it in array SUM_COL[M].



Calculate sum of each row the variable I should be inside and save it in array SUM_ROW[M].



Checking of sum of right diagonal SUM with the sum of each row in SUM_ROW[] array and jump if not equal. And same to be done with column.

        if(SUM != SUM_ROW[J])
                goto LABEL;


Print the Numbers of Given Array in the Matrix form by using Tab (\t) and New Line (\n) character in the nested Loop at the start of the program.


C program code :

  1. #include<stdio.h>
  2. #define MAX 10
  3. void main()
  4. {
  5.      int A[MAX][MAX],I,J,M,SUM=0,TEMP=0,SUM_COL[MAX],SUM_ROW[MAX];
  6.      clrscr();
  7.      printf("ENTER ORDER OF A SQUARE MATRIX  : \n");
  8.      printf("ENTER ORDER M (LESS THAN 10) :");
  9.      scanf("%d",&M);
  10.      printf("ENTER %d X %d MATRIX A VALUES\n",M,M);
  11.      for(I=0;I<M;I++)
  12.      {
  13.     for(J=0;J<M;J++)
  14.     {
  15.         scanf("%d",&A[I][J]);
  16.     }
  17.      }
  18.      printf("\nTHE GIVEN %d X %d MATRIX VALUES ARE :\n",M,M);
  19.      for(I=0;I<M;I++)
  20.      {
  21.     for(J=0;J<M;J++)
  22.     {
  23.         printf("%d\t",A[I][J]);
  24.     }
  25.     printf("\n");
  26.      }
  27.      for(I=0;I<M;I++)
  28.      {
  29.       SUM=SUM+A[I][I];
  30.      }
  31.      for(I=0,J=M-1;I<M;I++,J--)
  32.      {
  33.       TEMP=TEMP+A[I][J];
  34.      }
  35.      if(SUM==TEMP)
  36.      {
  37.     for(I=0;I<M;I++)
  38.     {
  39.         SUM_COL[I]=0;
  40.         for(J=0;J<M;J++)
  41.         {
  42.               SUM_COL[I]=SUM_COL[I]+A[I][J];
  43.         }
  44.     }
  45.     for(I=0;I<M;I++)
  46.     {
  47.         if(SUM != SUM_COL[I])
  48.               goto LABEL;
  49.     }
  50.     for(J=0;J<M;J++)
  51.     {
  52.         SUM_ROW[J]=0;
  53.         for(I=0;I<M;I++)
  54.         {
  55.               SUM_ROW[J]=SUM_ROW[J]+A[I][J];
  56.         }
  57.     }
  58.     for(J=0;J<M;J++)
  59.     {
  60.         if(SUM != SUM_ROW[J])
  61.                 goto LABEL;
  62.     }
  63.     printf("\nTHE GIVEN MATRIX IS A MAGIC SQUARE");
  64.      }
  65.      else
  66.      {
  68.      }
  69.      getch();
  70. }

Note:- whenever you have large number of inputs to be entered, we can use space instead of enter button all the time. You can see have its done in Output screen.




Leave a Reply