You are here

C program to check symmetric matrix

C program to check if a matrix is symmetric or not: we find transpose of the matrix and then compare it with the original matrix. For a symmetric matrix AT = A.

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.   int m, n, c, d, matrix[10][10], transpose[10][10];
  6.  
  7.   printf("Enter the number of rows and columns of matrix\n");
  8.   scanf("%d%d", &m, &n);
  9.   printf("Enter elements of the matrix\n");
  10.  
  11.   for (c = 0; c < m; c++)
  12.     for (d = 0; d < n; d++)
  13.       scanf("%d", &matrix[c][d]);
  14.  
  15.   for (c = 0; c < m; c++)
  16.     for (d = 0; d < n; d++)
  17.       transpose[d][c] = matrix[c][d];
  18.  
  19.   if (m == n) /* check if order is same */
  20.   {
  21.     for (c = 0; c < m; c++)
  22.     {
  23.       for (d = 0; d < m; d++)
  24.       {
  25.         if (matrix[c][d] != transpose[c][d])
  26.           break;
  27.       }
  28.       if (d != m)
  29.         break;
  30.     }
  31.     if (c == m)
  32.       printf("The matrix is symmetric.\n");
  33.     else
  34.       printf("The matrix isn't symmetric.\n");
  35.   }
  36.   else
  37.     printf("The matrix isn't symmetric.\n");
  38.  
  39.   return 0;
  40. }