Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

2D Array

Patterns

Excersises


C Program to Check Given Matrix is Symmetric or Not using 2D Array


Write a C program to Check if Matrix is Symmetric 2D array.

  • Takes input for a square matrix of size n x n from the user.
  • Traverses all elements to compare each element with its transposed counterpart
    matrix[i][j] == matrix[j][i].
  • Sets a flag isSymmetric to 0 if any pair doesn't match, breaking the check early.
  • Prints whether the matrix is symmetric based on the comparison results.
Example : pgm.c
#include <stdio.h>

int main() {
    int matrix[10][10], n, isSymmetric = 1;

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

    // Input matrix elements
    printf("Enter elements of the matrix:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Check for symmetry
    for (int i = 0; i < n && isSymmetric; i++) {
        for (int j = 0; j < n; j++) {
            if (matrix[i][j] != matrix[j][i]) {
                isSymmetric = 0;
                break;
            }
        }
    }

    // Output result
    if (isSymmetric)
        printf("The matrix is symmetric.\n");
    else
        printf("The matrix is not symmetric.\n");

    return 0;
}

Output:

Enter the size of the square matrix (n x n): 3
Enter elements of the matrix:
1 2 3
2 4 5
3 5 6
The matrix is symmetric.