The Program in C to find if a square matrix is symmetric is given below:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int matrix[n][n];
printf("Enter the elements of the matrix: \n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
int symmetric = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] != matrix[j][i]) {
symmetric = 0;
break;
}
}
}
if (symmetric) {
printf("The matrix is symmetric.\n");
} else {
printf("The matrix is not symmetric.\n");
}
return 0;
}
Output:
Enter the size of the square matrix: 4
Enter the elements of the matrix:
12
34
66
32
78
99
45
65
43
32
12
34
55
43
34
55
The matrix is not symmetric.
Pro-Tips💡
This program prompts the user to enter the size of a square matrix, and then the elements of the matrix.
It then checks if the matrix is symmetric by comparing each element in the matrix to its corresponding element in the transpose.
If all elements match, the matrix is symmetric, and the program prints “The matrix is symmetric.”
If any elements do not match, the matrix is not symmetric, and the program prints “The matrix is not symmetric.”
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.