Array Exercises
Write a C program to read elements in a matrix and check whether the given matrix is symmetric matrix or not. How to check symmetric matrix in C. Logic to check symmetric matrix in C programming.
Required knowledge
Basic C programming, C for loop, ArrayS
What is Symmetric Matrix?
Symmetric matrix is a square matrix which is equal to its transpose. A symmetric matrix is always a square matrix. Symmetric matrix A is defined as - A = AT

Logic to check symmetric matrix
To check whether a matrix A is symmetric or not we need to check whether A = AT or not. Below is the step by step descriptive logic to check symmetric matrix.
- Input elements in matrix A.
- Find transpose of matrix A, store it in some variable say B.
- Check if matrix A is equal to its transpose AT then it is symmetric matrix otherwise not. Means check if Aij = ATij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n) then the matrix is symmetric.
Program to check symmetric matrix
/** * C program to check whether a matrix is symmetric matrix or not */ #include <stdio.h> #define SIZE 3 int main() { int A[SIZE][SIZE]; // Original matrix int B[SIZE][SIZE]; // Transpose matrix int row, col, isSymmetric; /* Input elements in matrix A from user */ printf("Enter elements in matrix of size 3x3: \n"); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { scanf("%d", &A[row][col]); } } /* * Find transpose of matrix A */ for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { /* Store each row of matrix A to each column of matrix B */ B[row][col] = A[col][row]; } } /* * Check whether matrix A is equal to its transpose or not */ isSymmetric = 1; for(row=0; row<SIZE && isSymmetric; row++) { for(col=0; col<SIZE; col++) { /* If matrix A is not equal to its transpose */ if(A[row][col] != B[row][col]) { isSymmetric = 0; break; } } } /* * If the given matrix is symmetric. */ if(isSymmetric == 1) { printf("\nThe given matrix is Symmetric matrix: \n"); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { printf("%d ", A[row][col]); } printf("\n"); } } else { printf("\nThe given matrix is not Symmetric matrix."); } return 0; }
Output
Enter elements in matrix of size 3x3:
7 8 9
1 2 3
4 5 6
The given matrix is not Symmetric matrix.
Process returned 0 (0x0) execution time : 11.786 s
Enter elements in matrix of size 3x3:
10 20 30
20 30 40
30 40 50
The given matrix is Symmetric matrix:
10 20 30
20 30 40
30 40 50
Process returned 0 (0x0) execution time : 15.274 s
Program to check symmetric matrix
/** * C program to check whether a matrix is symmetric matrix or not */ #include <stdio.h> int main() { int A[10][10]; // Original matrix int B[10][10]; // Transpose matrix int row, col, isSymmetric,n; printf("Enter size of matrix:"); scanf("%d",&n); /* Input elements in matrix A from user */ printf("Enter elements in matrix of size 3x3: \n"); for(row=0; row<n; row++) { for(col=0; col<n; col++) { scanf("%d", &A[row][col]); } } /* * Find transpose of matrix A */ for(row=0; row<n; row++) { for(col=0; col<n; col++) { /* Store each row of matrix A to each column of matrix B */ B[row][col] = A[col][row]; } } /* * Check whether matrix A is equal to its transpose or not */ isSymmetric = 1; for(row=0; row<n && isSymmetric; row++) { for(col=0; col<n; col++) { /* If matrix A is not equal to its transpose */ if(A[row][col] != B[row][col]) { isSymmetric = 0; break; } } } /* * If the given matrix is symmetric. */ if(isSymmetric == 1) { printf("\nThe given matrix is Symmetric matrix: \n"); for(row=0; row<n; row++) { for(col=0; col<n; col++) { printf("%d ", A[row][col]); } printf("\n"); } } else { printf("\nThe given matrix is not Symmetric matrix."); } return 0; }