 # C Programming Code Examples

## C > Arrays and Matrices Code Examples

### program code to check symmetric matrix

``` program code to check symmetric matrix 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. 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 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. #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; } ``` 