Source code:
/*Write a C program to find out the Sum of the diagonal elements of a matrix. Consider a matrix as below:
1 2 3
4 5 6
7 8 9
The Sum of the diagonal elements of the above matrix is 15. */
#include<stdio.h>
int main(){
int a[3][3],i,j,sum=0,m=3,n=3;
/* Read the elements of the Matrix a */
printf("Enter a matrix:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("Sum of the Diagonal elements of the matrix=%d",sum);
return 0;
}
Output:
No comments:
Post a Comment