title: C - Solving Magic Square
body: <p>I am trying to create a program which checks whether a given matrix is a magic square or not. A magic square is where the sum of all the numbers in each of the sequences vertically, horizontally and diagonally are the same.
  Whatever combination of numbers I enter, it always returns Magic Square as true.
  Edit: Fixed diagonals (kinda), except it outputs additional info along with the diagonal.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  //#include &lt;stdbool.h&gt;
  
  int main() 
  {
  int nRows;
  int nCols;
  int sumR;
  int sumC;
  int sumD = 0;
  int sumD2 = 0;
  int k;
  int p;
  //bool isMagic = 1;
  
  
  //Defining Matrix order
  printf("Enter number of Rows: \n");
  scanf("%i",&amp;nRows);
  
  
  printf("Enter number of Columns: \n");
  scanf("%i",&amp;nCols);
  
  int matrix[nRows][nCols];
  //int SumR[nRows];
  //sint SumC[nCols];
  
  //Enterring Coefficients of the Matrix
  for(k = 0; k &lt; nRows; ++k){
      for(p = 0; p &lt; nCols; ++p){
          printf("Enter value for Matrix[%i][%i]: ",k,p);
  
          scanf("%i",&amp;matrix[k][p]);
      }
  }
  
  
  //Adding diagonal elements
  for(k = 0; k &lt; nRows; ++k){
      //for(p = 0; p &lt; nCols; ++p){
          //if(k == p ){
              sumD = sumD + matrix[k][k];
              sumD2 = sumD2 + matrix[k][nCols - k - 1];
              //sumD2 += matrix[k]
          //}
      //}
      printf("Sum of the Diagonals = %i\n",sumD);
      printf("Sum of the Diagonals2 = %i\n",sumD2);
      //sumD2 = 0;
      //sumD = 0;
  
  }
  
  
  //Adding the rows
  for(k = 0; k &lt; nRows; ++k){
      for(p = 0; p &lt; nCols; ++p){
          sumR += matrix[k][p];
      }
      printf("Sum of the %i row is = %i\n",k,sumR);
      //if(sumR != sumC)
          //isMagic = 0;
      sumR = 0;
  }
  
  //Adding the columns
  for(p = 0; p &lt; nRows; ++p){
      for(k = 0; k &lt; nCols; ++k){
          sumC += matrix[k][p];
      }
      printf("Sum of the %i column is = %i\n",p,sumC);
      //if(sumC != sumR)
          //isMagic = 0;
      sumC = 0;
  
  }
  
  
  //Displaying matrix
  for(k = 0; k &lt; nRows; ++k){
      for(p = 0; p &lt; nCols; ++p){
      printf("%i\t",matrix[k][p]);
  }
  printf("\n");
  } 
  
  
  
  if(sumR == sumC == sumD == sumD2){
      printf("\nMagic Square!\n");
  }
  else{
      printf("\nNot a Magic Square!\n");
  }
  
  
  return 0;
  //col[n]+row[n] == userinput
  
  }
  </code></pre>