What is a Transpose Matrix?
The transpose of a matrix is obtained by swapping its rows and columns. If A is a matrix, its transpose AT has elements AT[i][j]=A[j][i].
Examples
Here are some examples of matrices, one of which is a transpose(symmetric) matrix and the other is not.
Input : 1 5 3
5 1 8
3 8 3
Output: Yes
Input : 3 6 9
3 4 7
9 6 3
Output: No
Problem Description
Write a C Program to check whether a given matrix is symmetric or not
Approach 1
1) Create a transpose of the given matrix.
2) Check if the transpose and the given matrices are the same or not.
This is a simple brute-force solution, which we will use for loops.
CODE
C
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], b[10][10], c = 1;
printf("\nEnter the number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\nEnter the matrix elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
//Finding out the Transpose of matrix
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j; columns++)
{
b[columns][rows] = a[rows][columns];
}
}
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
if(a[rows][columns] != b[rows][columns])
{
c++;
break;
}
}
}
if(c == 1)
{
printf("\n The Matrix is a Symmetric Matrix. ");
}
else
{
printf("\n The Matrix is Not a Symmetric Matrix. ");
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
OUTPUT
Enter the number of rows and columns : 3 3
Enter the matrix elements
1 5 3
5 1 8
3 8 3
The Matrix is a Symmetric Matrix.
Explanation
We used For Loop to transpose the given Matrix. Next, we are trying to check whether each element in a matrix is equal to the transposed matrix or not. Thus for each element in the matrix, we are running the inner loop for n times and storing the transpose in another matrix, thereby needing space for that matrix as well.
Time Complexity: O(N x N)
Auxiliary Space: O(N x N)
You can practice by yourself with the help of online c compiler.
Approach 2
Comparing matrix elements without constructing a transpose is an efficient way to check if a matrix is symmetric or not. And for that we need to compare mat[i][j] with mat[j][i]. This is also a simple brute-force solution but the space complexity will not be as high as before.
CODE
C
#include <stdio.h>
int main()
{
int i, j, rows, columns, a[10][10]; int c=0;
printf("\nEnter the number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\nEnter the matrix elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
if (a[i][j] != a[j][i]){
c=1;
break;
}
}
}
if(c==1)
printf("\n The Matrix is Not a Symmetric Matrix. ");
else
printf("\n The Matrix is a Symmetric Matrix. ");
}

You can also try this code with Online C Compiler
Run Code
OUTPUT
Enter the number of rows and columns : 3 3
Enter the matrix elements
1 5 3
5 1 8
3 8 3
The Matrix is a Symmetric Matrix
Explanation
This is an efficient solution in terms of space as we are not creating any extra matrix to store the transpose of the given matrix. But the time complexity remains the same as we are running the inner loop n time for each element of the matrix.
Time Complexity: O(N x N)
Auxiliary Space: O(1)
Check out this problem - Matrix Median
Frequently Asked Questions
Why is C called a mid-level programming language?
C is a mid-level programming language because it bridges low-level and high-level computer languages. We may utilize the C language as a System programming language to create the operating system and an Application programming language to create a menu-driven customer-driven billing system.
What is a built-in function in C?
Built functions, also known as library functions, are supplied by the system to make a developer's life easier by supporting them in some commonly used predetermined activities. In C, for example, if we need to send the output of our program to the terminal, we would use printf().
scanf(), printf(), strcpy, strcmp, strlen, strcat, and many more built-in functions are the most widely used in C.
What are header files, and what are their uses in C?
In C, header files have the extension .h, these header files include function definitions, data type definitions, macros, and so on. The header is used to import the above definitions into the source code, this is done by using #include before the header. For example, if your source code requires taking input from the user, doing some modification, and printing the output on the terminal, it should include the stdio.h file as #include <stdio.h>, which allows the user to take input using scanf(), and to perform some manipulations, and after that print using printf().
What is a matrix in C programming?
A matrix is a rectangular array of numbers or symbols arranged in rows and columns.
How do you know if a matrix is an identity or not?
If the given matrix is a square matrix, loop through the array and see if all of the main diagonal elements are 1 and the rest are 0. Set the flag to false and break the loop if any of the conditions are not met. If the flag is set to true, the specified matrix is assumed to be an identity matrix.
Conclusion
In this blog, we explored how to check whether a given matrix is symmetric using a C program. A symmetric matrix is one where the transpose is equal to the original matrix (A=ATA = A^TA=AT).
By implementing a simple C program, we:
- Accepted a square matrix as input.
- Computed its transpose by swapping rows and columns.
- Compared the original matrix with its transpose to determine symmetry.
Recommended Readings: