C Program to Check Symmetric Matrix
matrix[i][j] with its transpose matrix[j][i]; if all pairs match, the matrix is symmetric.Examples
How to Think About It
i, j with the element at j, i. If all such pairs are equal, the matrix is symmetric; otherwise, it is not.Algorithm
Code
#include <stdio.h> int main() { int n, i, j, symmetric = 1; scanf("%d", &n); int matrix[n][n]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%d", &matrix[i][j]); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (matrix[i][j] != matrix[j][i]) { symmetric = 0; break; } } if (!symmetric) break; } if (symmetric) printf("The matrix is symmetric.\n"); else printf("The matrix is not symmetric.\n"); return 0; }
Dry Run
Let's trace the example matrix 3x3: [[1,2,3],[2,4,5],[3,5,6]] through the code.
Input matrix size and elements
n=3; matrix = [[1,2,3],[2,4,5],[3,5,6]]
Compare elements matrix[i][j] and matrix[j][i]
Check pairs: (0,1) 2==2, (0,2) 3==3, (1,2) 5==5 all match
All pairs matched
Set symmetric=1, print 'The matrix is symmetric.'
| i,j | matrix[i][j] | matrix[j][i] | Equal? |
|---|---|---|---|
| 0,0 | 1 | 1 | Yes |
| 0,1 | 2 | 2 | Yes |
| 0,2 | 3 | 3 | Yes |
| 1,0 | 2 | 2 | Yes |
| 1,1 | 4 | 4 | Yes |
| 1,2 | 5 | 5 | Yes |
| 2,0 | 3 | 3 | Yes |
| 2,1 | 5 | 5 | Yes |
| 2,2 | 6 | 6 | Yes |
Why This Works
Step 1: Matrix must be square
A symmetric matrix requires the same number of rows and columns so that matrix[i][j] and matrix[j][i] exist for all indices.
Step 2: Compare elements with transpose
Check if each element matches its transpose counterpart; if any pair differs, symmetry fails.
Step 3: Result based on comparisons
If all pairs match, print the matrix is symmetric; otherwise, print it is not.
Alternative Approaches
#include <stdio.h> int main() { int n, i, j, symmetric = 1; scanf("%d", &n); int matrix[n][n]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%d", &matrix[i][j]); for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (matrix[i][j] != matrix[j][i]) { symmetric = 0; break; } } if (!symmetric) break; } if (symmetric) printf("The matrix is symmetric.\n"); else printf("The matrix is not symmetric.\n"); return 0; }
#include <stdio.h> int isSymmetric(int n, int matrix[n][n]) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (matrix[i][j] != matrix[j][i]) return 0; } } return 1; } int main() { int n; scanf("%d", &n); int matrix[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &matrix[i][j]); if (isSymmetric(n, matrix)) printf("The matrix is symmetric.\n"); else printf("The matrix is not symmetric.\n"); return 0; }
Complexity: O(n^2) time, O(n^2) space
Time Complexity
The program uses nested loops over an n x n matrix, so it checks n^2 elements, resulting in O(n^2) time.
Space Complexity
The matrix is stored in a 2D array of size n x n, so space complexity is O(n^2).
Which Approach is Fastest?
Checking only the upper triangle reduces comparisons roughly by half but still remains O(n^2) time.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Full matrix check | O(n^2) | O(n^2) | Simple and clear code |
| Upper triangle check | O(n^2) | O(n^2) | Slightly faster by avoiding redundant checks |
| Function-based check | O(n^2) | O(n^2) | Better code organization and reuse |