C Program to check whether given Square Matrix is symmetric or not

48
Program to check whether given Square Matrix is symmetric or not

In this program, you’ll learn how to check whether the given Square Matrix asymmetric or asymmetric, if not then Display it’s not and if it’s symmetric display it’s symmetric

Before moving with the Program a few important things to remember.

  • A Square Matrix is said to be symmetric if it is equal to its transpose.
  • Transpose of a matrix is achieved by exchanging indices of rows and columns.
  • Transpose is only defined for a square matrix.

Below is a program to check a square matrix is symmetric or not.

Program to find whether the given Square Matrix is symmetric or not

#include<stdio.h>

int main()
{
    int c, d, a[10][10], b[10][10], n, temp;
    printf("\nEnter the dimension of the matrix: \n\n");
    scanf("%d", &n);

    printf("\nEnter the %d elements of the matrix: \n\n",n*n);
    for(c = 0; c < n; c++) // to iterate the rows
        for(d = 0; d < n; d++) // to iterate the columns
            scanf("%d", &a[c][d]);

    // finding transpose of a matrix and storing it in b[][]
    for(c = 0; c < n; c++) // to iterate the rows
        for(d = 0; d < n; d++) //to iterate the columns
            b[d][c] = a[c][d];

    // printing the original matrix
    printf("\n\nThe original matrix is: \n\n");
    for(c = 0; c < n; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", a[c][d]);
        }
    printf("\n");
    }

    // printing the transpose of the entered matrix
    printf("\n\nThe Transpose matrix is: \n\n");
    for(c = 0; c < n; c++) // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", b[c][d]);
        }
        printf("\n");
    }

    // checking if the original matrix is same as its transpose
    for(c = 0; c < n; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            /* 
                even if they differ by a single element, 
                the matrix is not symmetric
            */
            if(a[c][d] != b[c][d]) 
            {
                printf("\n\nMatrix is not Symmetric\n\n");
                exit(0);    // a system defined method to terminate the program
            }
        }
    }

    /* 
        if the program is not terminated yet, 
        it means the matrix is symmetric
    */
    printf("\n\nMatrix is Symmetric\n\n");
    return 0;
}

Output

Enter the dimension of the matrix:                                                                                              
                                                                                                                                
3                                                                                                                               
                                                                                                                                
Enter the 9 elements of the matrix:                                                                                             
                                                                                                                                
3 8 4 7 9 2 4 1 4 9                                                                                                             
                                                                                                                                
                                                                                                                                
The original matrix is:                                                                                                         
                                                                                                                                
3       8       4                                                                                                               
7       9       2                                                                                                               
4       1       4                                                                                                               
                                                                                                                                
                                                                                                                                
The Transpose matrix is:                                                                                                        
                                                                                                                                
3       7       4                                                                                                               
8       9       1                                                                                                               
4       2       4                                                                                                               
                                                                                                                                
                                                                                                                                
Matrix is not Symmetric
C Program to check whether given Square Matrix is symmetric or not
C Program to check whether given Square Matrix is symmetric or not

As the above Matrix example is Symmetric you can use different examples and test for its symmetry.

Ask your questions about the above Program. Documentation.

Please write to us at [email protected] to report any issue with the above content or for feedback.

Related Program