programming-examples/c/Matirix/C Program to find Sum of diagonal elements of a matrix.c
2019-11-15 12:59:38 +01:00

29 lines
767 B
C

#include<stdio.h>
void main()
{
int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0; i<m; i++)
{
printf("\n");
for(j=0; j<m; j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
printf("\nSum of the diagonal elements of a matrix is: %d",sum);
}