/* Trace of a matrix */
#include<stdio.h>
#include<math.h>
int main()
{
int i, j, n, m, a[20] [20], sum=0;
printf("Enter the order of matrix (mxn):\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of the matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &a[i] [j]);
}
}
printf("your matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%20d",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\ntrace of the matrix=%d\n",sum);
return(0);
}
#include<stdio.h>
#include<math.h>
int main()
{
int i, j, n, m, a[20] [20], sum=0;
printf("Enter the order of matrix (mxn):\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of the matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &a[i] [j]);
}
}
printf("your matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%20d",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\ntrace of the matrix=%d\n",sum);
return(0);
}
Comments
Post a Comment