Sum of left diagonal and right diagonal of a matrix.
#include<stdio.h>
int main() {
int a[3][3];
int sl=0,sr=0;
printf("Enter elements of an matrix : \n");
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
scanf("%d",&a[i][j]);
}
}
for (int i=0; i<3; i++){
sl=sl+a[i][i];
sr+=a[i][2-i];
}
printf("sum of left diagonal : %d\nsum of right diagonal : %d",sl,sr);
return 0;
}
Output:-
Enter elements of an matrix :
1 5 4
2 3 4
2 5 4
sum of left diagonal : 8
sum of right diagonal : 9