C Program to Check Symmetric Matrix

  • Write a C program to check whether a matrix is symmetric matrix or not.

Required Knowledge


This program checks whether a given matrix is symmetric matrix or not. Here is the algorithm to check symmetric matrix.

Algorithm to find symmetric matrix
Let inputMatrix is an integer matrix having N rows and M columns.

C program to check a matrix is symmetric matrix or not

#include <stdio.h>
#include <conio.h>
 
int main(){
    int rows, cols, row, col, size, isSymmetric;
    int inputMatrix[50][50], transposeMatrix[50][50];
    
    printf("Enter the size of Square Matrix\n");
    scanf("%d", &size);
    rows = cols = size;
    
    printf("Enter Matrix of size %dX%d\n", rows, cols);
     
    for(row = 0; row < rows; row++){
        for(col = 0; col < cols; col++){
            scanf("%d", &inputMatrix[row][col]);
        }
    }
     
    /* Find Transpose of inputMatrix 
 transpose[i][j] = inputMatrix[j][i] */
    for(row = 0; row < rows; row++){
        for(col = 0; col < cols; col++){
            transposeMatrix[col][row] = inputMatrix[row][col];
        }
    }
     
    /* Compare Input Matrix and its Transpose Matrix */
    isSymmetric = 1;
    for(row = 0; row < cols; row++){
        for(col = 0; col < rows; col++){
            if(inputMatrix[row][col] != transposeMatrix[row][col]){
                isSymmetric = 0;
            }
        }
    }
    
    if(isSymmetric == 1)
        printf("Input Matrix is Symmetric Matrix\n");
    else 
        printf("Input Matrix is Not a Symmetric Matrix\n");
    
    getch();
    return 0;
}
Output
Enter the size of Square Matrix
3
Enter Matrix of size 3X3
4 5 6
5 9 1
6 1 2
Input Matrix is Symmetric Matrix
Enter the size of Square Matrix
3
Enter Matrix of size 3X3
1 2 3
4 5 6
7 8 9
Input Matrix is Not a Symmetric Matrix

Related Topics
C Program to find sum of upper triangular matrix
C Program to find transpose of matrix
C program to check identity matrix
C program to find scalar multiplication of a matrix
C Program to find sum of diagonal elements of matrix
C program to print lower triangular matrix
C program to print upper triangular matrix ween 1 to N
C Program to find sum of lower triangular matrix
C program to compare two matrix
List of all C programs