133. C Program to Find the Trace of A Matrix.
How does this program work?
- In this programme you will learn about how to find trace of matrix using C Language.
- variables are stored in two arrays.
- And then by using formula finding the trace of matrix.
Here is the code
#include <stdio.h>
int main(void)
{
int i,j,rows,col,trace = 0;
printf("Enter number of rows and columns of a matrix\n");
scanf("%d %d",&rows,&col);
int a[rows][col];
if(rows==col)
{
//Taking input of matrix
printf("Enter Matrix 1\n");
for(i=0;i< rows;i++)
{
for( j=0;j< col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Given matrix is\n");
for(i=0;i< rows;i++)
{
for(j=0;j< col;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i< rows;i++)
{
trace+=a[i][i];
}
printf("Trace of above matrix is %d\n",trace);
}
else
{
printf("Trace is not possible \n");
}
return 0;
}