Thursday 29 December 2011

C program to find the sum of the diagonal elements(Trace) of a matrix



#include<stdio.h>
main()
{
int a[10][10],s,i,j,r,c;
printf("Enter the order of the matrix\n");
scanf("%d%d",&r,&c);
printf("Enter the values\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
s=0;
if(r==c)
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
s=s+a[i][j];
}
}
printf("\nSum of diagonal elements = %d",s);
}
else
printf("\nNo diagonal elements");
printf("\n");
}



You Like It!? Then kindly share with your Friends.
Comments
1 Comments

1 comment:

Unknown said...

Good explanation

Post a Comment