Check Whether a Matrix Is Symmetric in C

This program checks whether a given n x n matrix is a symmetric matrix — meaning the matrix equals its own transpose — and displays the result along with the original matrix.

C Program

#include <stdio.h>

int main() {
    int n, i, j;
    int a[10][10];
    int isSymmetric = 1;

    printf("Enter order of square matrix (n): ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n * n);
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            if (a[i][j] != a[j][i]) {
                isSymmetric = 0;
            }
        }
    }

    printf("\nOriginal Matrix:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    if (isSymmetric) {
        printf("\nSymmetric\n");
    } else {
        printf("\nNot Symmetric\n");
    }

    return 0;
}

Explanation

  • A matrix is symmetric if a[i][j] == a[j][i] for every pair of indices — i.e., it looks the same when reflected across its main diagonal.
  • The nested loop checks every element against its "mirror" element. If any pair doesn't match, isSymmetric is set to 0.
  • The original matrix is printed regardless of the result, followed by "Symmetric" or "Not Symmetric".

Sample Output

Enter order of square matrix (n): 3
Enter 9 elements:
1 2 3
2 5 6
3 6 9

Original Matrix:
1 2 3
2 5 6
3 6 9

Symmetric

Leave a Comment