sum of diagonal elements of a matrix in c
#include<stdio.h>
void main()
{
int mat[12][12];
int i,j,row,col,sum=0;
printf("Enter the number of rows and columns for 1st matrixn");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matrixn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("The matrixn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%dt",mat[i][j]);
}
printf("n");
}
//To add diagonal elements
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+mat[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix = %dn",sum);
}