C program to check symmetric matrix

Write a C program to read elements in a matrix and check whether the given matrix is symmetric matrix or not. How to check symmetric matrix in C. Logic to check symmetric matrix in C programming.

Example

Input

Input matrix elements: 
1 2 3
2 4 5
3 5 8

Output

Given matrix is symmetric matrix.

Required knowledge

Basic C programming, For loop, Array

Must know -

What is Symmetric Matrix?

Symmetric matrix is a square matrix which is equal to its transpose. A symmetric matrix is always a square matrix. Symmetric matrix A is defined as - A = AT

Symmetric matrix

Logic to check symmetric matrix

To check whether a matrix A is symmetric or not we need to check whether A = AT or not. Below is the step by step descriptive logic to check symmetric matrix.

  1. Input elements in matrix A.
  2. Find transpose of matrix A, store it in some variable say B.
  3. Check if matrix A is equal to its transpose AT then it is symmetric matrix otherwise not. Means check if Aij = ATij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n) then the matrix is symmetric.

Program to check symmetric matrix

/**
 * C program to check whether a matrix is symmetric matrix or not
 */

#include <stdio.h>
#define SIZE 3

int main()
{
    int A[SIZE][SIZE];  // Original matrix
    int B[SIZE][SIZE];  // Transpose matrix

    int row, col, isSymmetric;

    /* Input elements in matrix A from user */
    printf("Enter elements in matrix of size 3x3: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /*
     * Find transpose of matrix A
     */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /* Store each row of matrix A to each column of matrix B */
            B[row][col] = A[col][row];
        }
    }


    /*
     * Check whether matrix A is equal to its transpose or not
     */
    isSymmetric = 1;
    for(row=0; row<SIZE && isSymmetric; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /* If matrix A is not equal to its transpose */
            if(A[row][col] != B[row][col])
            {
                isSymmetric = 0;
                break;
            }
        }
    }

    /*
     * If the given matrix is symmetric.
     */
    if(isSymmetric == 1)
    {
        printf("\nThe given matrix is Symmetric matrix: \n");

        for(row=0; row<SIZE; row++)
        {
            for(col=0; col<SIZE; col++)
            {
                printf("%d ", A[row][col]);
            }

            printf("\n");
        }
    }
    else
    {
        printf("\nThe given matrix is not Symmetric matrix.");
    }

    return 0;
}

Output

Enter elements in matrix of size 3x3:
1 2 3
2 4 5
3 5 8

The given matrix is Symmetric matrix:
1 2 3
2 4 5
3 5 8

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>