**Symmetric Matrix:** A square matrix that is equal to its transpose is known as a symmetric matrix.

**Transpose Matrix:** The transpose of a matrix is found by interchanging its rows into columns or columns into rows. The transpose of the matrix is denoted by using the letter “**T**”.

So, a matrix **A** is said to be symmetric if it is equal to its transpose i.e., **A=A ^{T}**.

**Example 1: **

If the given matrix \( A =

\begin{bmatrix}

1 & 4 & 5\\

4 & 3 & 2\\

5 & 2 & 1

\end{bmatrix}\), then **(A ^{T})** is \(
\begin{bmatrix}

1 & 4 & 5\\

4 & 3 & 2\\

5 & 2 & 1

\end{bmatrix}\)

Since A = A^{T}, therefore it is a Symmetric Matrix.

**Example 2: **

If the given matrix \( A =

\begin{bmatrix}

2 & 5 & 9\\

1 & 2 & 3\\

4 & 7 & 2

\end{bmatrix}\), then **(A ^{T})** is \(
\begin{bmatrix}

2 & 1 & 4\\

5 & 2 & 7\\

9 & 3 & 2

\end{bmatrix}\)

Since A ≠ A^{T}, therefore it is not a Symmetric Matrix.

Write a C Program to check whether a given matrix is symmetric or not.

1. Enter the number of rows and columns of a matrix.

2. If number of rows not equal to number of columns print as non-symmetric matrix.

3. Else Enter elements in the **matrix A**.

4. Find transpose of the matrix and store it in another **array B**.

5. Check if **A** is equal to its **transpose B**.

6. If **A = B** then it is Symmetric else Non-symmetric.

Here is source code of the C program to check matrix is a symmetric matrix or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C Program to Check Matrix is a Symmetric Matrix or Not */ #include<stdio.h> #include<stdlib.h> int main() { int i, j, row, col, count = 0; printf("Please Enter Number of rows and columns: "); scanf("%d %d", &i, &j); if(i!=j) { printf("Rows not equal to columns. Therefore Non-Symmetric Matrix."); exit(0); } //Initialize 2d-array of size i,j. int a[i][j], b[i][j]; printf("\nEnter the Matrix Elements \n"); for(row = 0; row < i; row++) { for(col = 0;col < j;col++) { scanf("%d", &a[row][col]); } } //Transpose of matrix for(row = 0; row < i; row++) { for(col = 0;col < j; col++) { b[col][row] = a[row][col]; } } //Check if matrix a equals to matrix b or not. for(row = 0; row < i; row++) { for(col = 0; col < j; col++) { if(a[row][col] != b[row][col]) { count++; break; } } } if(count == 0) { printf("\nThe given Matrix is a Symmetric Matrix "); } else { printf("\nThe given Matrix is Not a Symmetric Matrix "); } return 0; }

1. Ask the user to enter number of **rows** and **columns** in a matrix and store them in the variables **i** and **j** respectively.

2. Initialize an integer variable **count=0**.

3. If **i** not equal to **j** print it as **Non-Symmetric Matrix** and exit the program.

4. Else initialize two 2D array/ Matrix of size **i, j i.e., a[i][j] and b[i]j[j]**.

5. Ask the user to enter the elements in the **matrix a**.

6. Calculate the transpose of the matrix by running 2 for loops and store it in the **matrix b**. To calculate transpose of the **matrix a** do, **b[col][row] = a[row][col]** in each iteration of the loop.

7. After calculating the **transpose b** check if **matrix a** equals to **matrix b** or not.

8. For checking run 2 loops from row = 0 to i and col = 0 to i and in each iteration check if **b[row][col]** is equal to **a[row][col]** or not. If it is not equal increase the value of count by 1 and break from the loop.

9. Now, check if value of **count = 0** or not. If it is equal to 0 print the matrix is a symmetric matrix else print the matrix is non-symmetric.

**Time Complexity: O(n ^{2})**

The above program for checking whether a matrix is symmetric or not has a time complexity of O(n

^{2}), as the for loop runs for n

^{2}times.

**Space Complexity: O(n ^{2})**

In the above program, space complexity is O(n

^{2}) as 2D-arrays of size n have been initialized to store the values in it and the space complexity of 2D array is O(n

^{2}).

**Testcase 1: ** In this case, we enter “3” for the number of rows and “4” for the number of columns to check whether the matrix is symmetric or not.

Please Enter Number of rows and columns: 3 4 Rows not equal to columns. Therefore Non-Symmetric Matrix.

**Testcase 2: ** In this case, we enter “3” for the number of rows and “3” for the number of columns to check whether the matrix is symmetric or not.

Please Enter Number of rows and columns: 3 3 Enter the Matrix Elements 1 4 5 4 3 2 5 2 1 The given Matrix is a Symmetric Matrix

**Testcase 3: ** In this case, we enter “4” for the number of rows and “4” for the number of columns to check whether the matrix is symmetric or not.

Please Enter Number of rows and columns: 4 4 Enter the Matrix Elements 1 2 3 4 2 4 7 8 3 5 9 2 4 8 5 2 The given Matrix is Not a Symmetric Matrix

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Apply for C Internship
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Practice BCA MCQs
- Watch Advanced C Programming Videos