Symmetric Matrix C Program

By | October 28, 2016

Learn How To Find if a Matrix is a Symmetric Matrix in C Programming. The following C programs use functions, arrays and Transpose concepts to check if a Square Matrix is Symmetric or not.

What is a Symmetric Matrix?

A Symmetric Matrix is the one that is always equivalent to its Transpose. A Square Matrix that is identical to its Transpose Matrix is known as a Symmetric Matrix. Only square matrices are symmetric as equal matrices have equal dimensions.

Example

Input Matrix

1 5 7
5 4 9
7 9 4

Transpose Matrix

1 5 7
5 4 9
7 9 4

Since the Input Matrix and the Transpose are equivalent, the Matrix is Symmetrical.

Algorithm To Find Symmetric Matrix

 

  1. Input the Matrix from the User.
  2. Find the Transpose of the Matrix.
  3. If the Input Matrix and its Transpose are same, then the Matrix is Symmetrical.

Method 1: C Program To Find if a Matrix is a Symmetric Matrix or Not without Functions

#include<stdio.h>

int main()
{
      int arr[5][5];
      int row, column, row_limit, column_limit, temp = 0;
      printf("Enter the Number of Rows:\t");
      scanf("%d", &row_limit);
      printf("Enter the Number of Columns:\t");
      scanf("%d", &column_limit);
      printf("\nEnter the Elements of the Square Matrix of Dimension [%d][%d]\n", row_limit, column_limit);
      for(row = 0; row < row_limit; row++)
      {
            for(column = 0; column < column_limit; column++)
            {
                  printf("Enter Element:\t"); 
                  scanf("%d", &arr[row][column]);
            }
      }
      printf("\nEntered Matrix\n");
      for(row = 0; row < row_limit; row++)
      {
            for(column = 0; column < column_limit; column++)
            {
                  printf("%d\t", arr[row][column]);
            }
            printf("\n");
      }
      for(row = 0; row < row_limit; row++)
      {
            for(column = 0; column < column_limit; column++)
            {
                  if(arr[row][column] != arr[column][row])
                  {
                        temp = 1;
                        break;
                  }
            }
      }
      if(temp == 1)
      {
            printf("\nThe Entered Matrix is Not A Symmetric Matrix\n");
      }
      else
      {
            printf("\nThe Entered Matrix is A Symmetric Matrix\n");
      }         
      return 0;
}

Method 2: C Program To Find Symmetric Matrix using Array and Transpose

#include<stdio.h>
 
int main()
{
      int arr[10][10], transpose[10][10];
      int row_limit, column_limit, row, column;
      printf("Enter the Number of Rows:\t");
      scanf("%d", &row_limit);
      printf("Enter the Number of Columns:\t");
      scanf("%d", &column_limit);
      printf("\nEnter the Elements of the Square Matrix of Dimension [%d][%d]\n", row_limit, column_limit);
      for(row = 0; row < row_limit; row++)
      {
            for(column = 0; column < column_limit; column++)
            {
                  printf("Enter Element:\t"); 
                  scanf("%d", &arr[row][column]);
            }
      }
      for(row = 0 ; row < row_limit ; row++)
      {
            for(column = 0; column < column_limit; column++)
            {
                  transpose[column][row] = arr[row][column];
            }
      }
      if(row_limit == column_limit)
      {
            for(row = 0; row < row_limit; row++)
            {
                  for(column = 0; column < row_limit; column++)
                  {
                        if(arr[row][column] != transpose[row][column])
                        {
                              break;
                        }
                  }
                  if(column != row_limit)
                  {
                        break;
                  }
            }
            if(row == row_limit)
            {
                  printf("\nThe Entered Matrix is A Symmetric Matrix\n");
            }
      }
      else
      {
            printf("\nThe Entered Matrix is Not A Symmetric Matrix\n");
      }
      return 0;
}

Method 3: C Program To Check Symmetric Matrix using Functions

#include<stdio.h>
 
int arr[5][5], transpose[5][5];
int row_limit, column_limit, row, column;

void getmatrix()
{
      for(row = 0; row < row_limit; row++)
      {
            for(column = 0; column < column_limit; column++)
            {
                  printf("Enter Element:\t"); 
                  scanf("%d", &arr[row][column]);
            }
      }
}

void find_transpose()
{
      for(row = 0 ; row < row_limit ; row++)
      {
            for(column = 0; column < column_limit; column++)
            {
                  transpose[column][row] = arr[row][column];
            }
      }
}

int check()
{
      if(row_limit == column_limit)
      {
            for(row = 0; row < row_limit; row++)
            {
                  for(column = 0; column < row_limit; column++)
                  {
                        if(arr[row][column] != transpose[row][column])
                        {
                              break;
                        }
                  }
                  if(column != row_limit)
                  {
                        break;
                  }
            }
            if(row == row_limit)
            {
                  return 0;
            }
      }
      else
      {
            return 1;
      }
}

int main()
{
      int result;
      printf("Enter the Number of Rows:\t");
      scanf("%d", &row_limit);
      printf("Enter the Number of Columns:\t");
      scanf("%d", &column_limit);
      printf("\nEnter the Elements of the Square Matrix of Dimension [%d][%d]\n", row_limit, column_limit);
      getmatrix();
      find_transpose();
      result = check();
      if(result == 0)
      {
            printf("\nThe Entered Matrix is A Symmetric Matrix\n");
      }
      else
      {
            printf("\nThe Entered Matrix is Not A Symmetric Matrix\n");
      }
      return 0;
}

Output

C Program To Check Symmetric Matrix with and without Function and Array

In case you get any compilation errors or any doubts in this C Program To Check if a Matrix is Symmetrical or Not, let us know about it in the comment section below.

Recommended Programs
C Program To Find Transpose of a Matrix
C Program To Find Sum of Minor Diagonal Elements
C Program To Find Trace of Matrix
C Program To Display India’s Map
C Program To Find IP Address
C Program To Create Custom Header Files
C Program To Calculate Sum of Rows and Columns of Matrix
C Program To Find Transpose of a Matrix
C Program To Find Sum of Lower Trianglular Elements
C Program To Print Pascal’s Triangle

Let's Discuss