Program to Check Symmetric Matrix in C


Program Description:

Write a program to Check Symmetric Matrix in C programming language. The program will accept a Matrix from the user and check if it is a Symmetric Matrix.

What is a Symmetric Matrix?

A Matrix is called a Symmetric Matrix When the Transpose of the Matrix is equal to the Original Matrix.

The Symmetric Matrix is a Square Matrix.

If a Matrix X is said to be symmetric when

X = XT

Where X is an Original Matrix and XT is the Transpose of the X.

Let’s look at an example to understand the symmetric matrix.

Original Matrix : X:

Transpose of X: XT

Here is the graphical representation of the symmetric matrix


Let’s look at an example program to check a symmetric matrix in the c programming language


It is recommended to know the Arrays in C language and Multi-dimensional arrays. Please go through the following articles to learn more about these topics.

Algorithm to check the Symmetric Matrix:

As the Symmetric matrix’s Transpose is equal to the Original Matrix. So we need to calculate the Transpose and compare the two matrices.

Step 1: Calculate the Transpose of the Matrix (XT)

Step 2: Check if the Transpose (XT) and Original Matrices(X) are Equal

Check Symmetric Matrix in C Program Explanation:

Here is a step-by-step explanation of the check symmetric matrix program.

  1. Create two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns in the Matrix. Please change this number if you want to play with the large arrays.
  2. Start the Program by declaring two Matrices (2D Arrays). They are X matrix, and XTranspose Matrix. The row size and column size of the matrices are ROWS and COLUMNS constants respectively.
  3. Prompt the user to provide the desired Row size and Column Size, and store them in the variables rows and columns respectively.
  4. The Symmetric matrix must be a Square Matrix. So check if the rows and columns are equal – if(rows != columns)
  5. Check for array bounds using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the present limits.
  6. Take the user input for Input Matrix i.e Matrix-X, We need to use two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
  7. Create the First For Loop ( Outer Loop), Start the iteration from i=0 and go till the i<rows (that is for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). The Inner Loop will iterate over the columns. At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix element and Read the element using the scanf function and Update the X[i][j] element.
      • Repeat the above step for all elements in the Matrix-X
  8. Now, To Calculate the Transpose of the X, We need to use two more for loops.
  9. Create Outer For Loop ( Iterates over rows) – for(i=0; i<rows; i++)
    • Then Create the Inner For loop (Iterates over columns) – for(j=0; j<columns; j++)
      • Copy the value of the X[i][j] element to the XTranspose[j][i]. Note that, i and j are interchanged.
      • i.e XTranspose[j][i] = X[i][j];
  10. Once the step-8 is completed, The XTranspose Matrix contains the Transpose of the X (Matrix).
  11. Now, We need to Check if the two matrices (X and XTranspose) are Identical.
  12. Create Outer For Loop to iterate over rows – for(i=0; i<rows; i++)
    • Create Inner For loop to iterate over columns – for(j=0; j<columns; j++), At Each Iteration,
      • Check if the X[i][j] element is equal to XTranspose[i][j]
        element – if(X[i][j] != <code>XTranspose[i][j])
      • If any element pair is not equal, Then Matrix X and XTranspose are not Identical. Thus Input Matrix X is not a Symmetric Matrix.
  13. If the Matrix X and XTranspose are Equal, Then Input matrix X is a Symmetric Matrix.
  14. Stop the Program.

Program to Check Symmetric Matrix in C Language using Loops:

Here is the program to check the symmetric matrix in the C programming language.

Program Output:

Let’s compile and run the program.

Compile the program

$ gcc check-symmetric-matrix.c

Run the Program

Test 1: Normal Cases:

From the above output, We can see that The input matrix is a Symmetric Matrix as its transpose and original matrices are equal.

Let’s look at a few more examples.


Test 2: Negative tests:

The program displays an error message if the input matrix is not a square matrix or if sizes go beyond the present limits.

Check Symmetric Matrix using Functions in C Program:

We have used only one function to do all the tasks in the above program. Let’s use the functions for each sub-tasks like reading the matrix from the user, printing the matrix on the console, checking the symmetric matrix, etc.

Here is the modified version of the symmetric matrix checking program using user-defined functions.

We have defined four user-defined functions in the above program (excluding the main() function)

They are

  1. The readMatrix() Function.
    • Prototype: void readMatrix(int rows, int columns, int A[rows][columns])
    • readMatrix() function is used to read the user input and update the input matrix
  2. The displayMatrix() Function
    • Prototype: void displayMatrix(int rows, int columns, int A[rows][columns])
    • The displayMatrix() function is used to display the elements of the matrix on the console
  3. The calculateTranspose() Function.
    • Prototype: void calculateTranspose(int rows, int columns, int X[rows][columns], int XTranspose[columns][rows])
    • The calculateTranspose() function will calculate the transpose of the given matrix and updates the XTranspose Matrix
  4. The isSymmetricMatrix() function
    • Prototype: int isSymmetricMatrix(int rows, int columns, int X[rows][columns], int XTranspose[rows][columns])
    • The isSymmetricMatrix() function checks if the X and XTranspose matrices are identical, Thus the X is a Symmetric Matrix.
    • This function returns One(1) if the X is a Symmetric matrix, Otherwise it returns Zero(0)

Program Output:

Let’s compile and Run the Program using GCC compiler(Any Compiler)

Test 1:


Test 2:

As we can see from the above outputs, The program is generating the excepted results.

Related Matrices Programs:


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply