Tuesday, June 7, 2011

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

/* Concept:-  A matrix is symmetric if its transpose is same as the matrix itself*/

#include<stdio.h>
#include<conio.h>

//write matrix

void write_mat(int a[][10],int n)
{
int i,j;
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%10d",a[i][j]);
printf("\n");
}
}

//read matrix

void read_mat(int a[][10],int n)
{
int i,j;
printf("Enter %d X %d matrix below:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}

//main function

void main()
{
int a[10][10],i,j,n;

//accept matrix

printf("Enter size of square matrix");
scanf("%d",&n);
read_mat(a,n);

//check if the matrix is symmetric or not

for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i][j]!=a[j][i])
{
printf("Not symmetric");
return;
}
printf("Symmetric");
getch();
}

No comments:

Post a Comment