Square matrix is symmetric or not
C program to check whether given square matrix is symmetric or not.
A square matrix is called symmetric matrix if that matrix is equal to its transposed matrix. A symmetric matrix is always a square matrix. Symmetric matrix A is defined as: A = AT
Program
#include<stdio.h>
#include<conio.h>
#define MAX 10
void read_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Element [%d][%d]\t:",i,j);
scanf("%d",&mat[i][j]);
}
}
}
void print_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void symmetric(int mat[MAX][MAX], int row)
{
int i, j, trans[MAX][MAX], flag;
for(i=0;i<row;i++)
{
for(j=0;j<row;j++)
{
trans[j][i] = mat[i][j];
}
}
flag = 0;
for(i=0;i<row;i++)
{
for(j=0;j<row;j++)
{
if(mat[i][j] != trans[i][j])
{
flag = 1;
break;
}
}
}
if(flag == 0)
{
printf("\nMatrix is symmetric matrix");
}
else
{
printf("\nMatrix is not a symmetric matrix");
}
}
void main()
{
int mat[MAX][MAX];
int row, col;
printf("Enter the number of rows or columns of the square matrix\t:");
scanf("%d",&row);
col = row;
printf("Enter the elements of the matrix\n");
read_matrix(mat, row, col);
printf("\nMatrix entered is\n");
print_matrix(mat, row, col);
symmetric(mat, row);
getch();
}
Output
********** Run1 **********
Enter the number of rows or columns of the square matrix :4
Enter the elements of the matrix
Element [0][0] :9
Element [0][1] :13
Element [0][2] :3
Element [0][3] :6
Element [1][0] :13
Element [1][1] :11
Element [1][2] :7
Element [1][3] :6
Element [2][0] :3
Element [2][1] :7
Element [2][2] :4
Element [2][3] :7
Element [3][0] :6
Element [3][1] :6
Element [3][2] :7
Element [3][3] :10
Matrix entered is
9 13 3 6
13 11 7 6
3 7 4 7
6 6 7 10
Matrix is symmetric matrix
********** Run2 **********
Enter the number of rows or columns of the square matrix :3
Enter the elements of the matrix
Element [0][0] :1
Element [0][1] :2
Element [0][2] :3
Element [1][0] :4
Element [1][1] :5
Element [1][2] :6
Element [2][0] :7
Element [2][1] :8
Element [2][2] :9
Matrix entered is
1 2 3
4 5 6
7 8 9
Matrix is not a symmetric matrix