0
0
CProgramBeginner · 2 min read

C Program to Check Symmetric Matrix

A C program to check a symmetric matrix reads a square matrix and uses nested loops to compare each element matrix[i][j] with its transpose matrix[j][i]; if all pairs match, the matrix is symmetric.
📋

Examples

Input3 1 2 3 2 4 5 3 5 6
OutputThe matrix is symmetric.
Input2 1 0 2 1
OutputThe matrix is not symmetric.
Input1 7
OutputThe matrix is symmetric.
🧠

How to Think About It

To check if a matrix is symmetric, first ensure it is square. Then, compare each element at position i, j with the element at j, i. If all such pairs are equal, the matrix is symmetric; otherwise, it is not.
📐

Algorithm

1
Get the size of the square matrix.
2
Read the matrix elements.
3
For each element at row i and column j, compare it with the element at row j and column i.
4
If any pair does not match, conclude the matrix is not symmetric and stop.
5
If all pairs match, conclude the matrix is symmetric.
💻

Code

c
#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;
}
Output
The matrix is symmetric.
🔍

Dry Run

Let's trace the example matrix 3x3: [[1,2,3],[2,4,5],[3,5,6]] through the code.

1

Input matrix size and elements

n=3; matrix = [[1,2,3],[2,4,5],[3,5,6]]

2

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

3

All pairs matched

Set symmetric=1, print 'The matrix is symmetric.'

i,jmatrix[i][j]matrix[j][i]Equal?
0,011Yes
0,122Yes
0,233Yes
1,022Yes
1,144Yes
1,255Yes
2,033Yes
2,155Yes
2,266Yes
💡

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

Check only upper triangle
c
#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;
}
This reduces redundant checks by comparing only elements above the main diagonal.
Use a function to check symmetry
c
#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;
}
Separates logic into a function for better readability and reuse.

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.

ApproachTimeSpaceBest For
Full matrix checkO(n^2)O(n^2)Simple and clear code
Upper triangle checkO(n^2)O(n^2)Slightly faster by avoiding redundant checks
Function-based checkO(n^2)O(n^2)Better code organization and reuse
💡
Only square matrices can be symmetric, so always check matrix size first.
⚠️
Beginners often forget to check if the matrix is square before testing symmetry.