C# program to check if a matrix is symmetric

In this article, we are going to discuss how we can check if a matrix is symmetric or not using C#.

What is a Symmetric Matrix?

A symmetric matrix is a square matrix (matrix which has the same number of rows and columns) in which the element at position A[i][j] is equal to the element at A[j][i] for all i and j. In simple words, a matrix is called symmetric if it is equal to its transpose.

Symmetric Matrix Property Original Matrix 1 2 3 2 4 5 3 5 6 = Transpose 1 2 3 2 4 5 3 5 6 A[i][j] = A[j][i] for all valid i, j

Examples

Input

1 2 3
2 4 5
3 5 6

Output: True

Explanation: The above matrix is a square matrix and for every element matrix[i][j] equals matrix[j][i]. So, the matrix is symmetric.

Input

1 2 3 4
5 6 7 8
9 10 11 12

Output: False

Explanation: The above matrix is not a square matrix (3×4), so it cannot be symmetric.

Syntax

To check if a matrix is symmetric, we use the following condition

if (matrix[i, j] == matrix[j, i]) {
   // Elements are symmetric
}

We also need to verify it's a square matrix first

if (rows != cols) {
   // Not a square matrix, cannot be symmetric
}

Using Brute Force Approach

This is a straightforward approach where we compare every element matrix[i][j] with matrix[j][i] for all valid indices

using System;

class SymmetricMatrix {
    static void Main() {
        int[,] matrix = {
            { 2, 1, 3 },
            { 1, 4, 5 },
            { 3, 5, 6 }
        };

        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        if (rows != cols) {
            Console.WriteLine("The matrix is not square, so it cannot be symmetric.");
            return;
        }

        bool isSymmetric = true;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix[i, j] != matrix[j, i]) {
                    isSymmetric = false;
                    break;
                }
            }
            if (!isSymmetric) break;
        }

        Console.WriteLine(isSymmetric ? "The matrix is symmetric." : "The matrix is not symmetric.");
    }
}

The output of the above code is

The matrix is symmetric.

Time Complexity: O(n²), as we use nested loops to traverse the entire matrix.

Space Complexity: O(1), constant space.

Using Upper Triangle Comparison

This optimized approach only compares elements in the upper triangle of the matrix, reducing the number of comparisons by half

using System;

class SymmetricMatrixOptimized {
    static void Main() {
        int[,] matrix = {
            { 1, 2, 3 },
            { 2, 4, 5 },
            { 3, 5, 6 }
        };

        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        if (rows != cols) {
            Console.WriteLine("The matrix is not square, so it cannot be symmetric.");
            return;
        }

        bool isSymmetric = true;

        for (int i = 0; i < rows; i++) {
            for (int j = i + 1; j < cols; j++) {
                if (matrix[i, j] != matrix[j, i]) {
                    isSymmetric = false;
                    break;
                }
            }
            if (!isSymmetric) break;
        }

        Console.WriteLine(isSymmetric ? "The matrix is symmetric." : "The matrix is not symmetric.");
    }
}

The output of the above code is

The matrix is symmetric.

Time Complexity: O(n²/2), as we only traverse the upper triangle elements.

Space Complexity: O(1), constant space.

Comparison

Approach Time Complexity Space Complexity Comparisons
Brute Force O(n²) O(1) All n² elements
Upper Triangle O(n²/2) O(1) Only upper triangle

Conclusion

A symmetric matrix is a square matrix where A[i][j] equals A[j][i] for all valid indices. The upper triangle comparison approach is more efficient as it reduces the number of comparisons by half while maintaining the same result accuracy.

Updated on: 2026-03-17T07:04:36+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements