/* Sum of diagonal elements of a matrix. */
#include <stdio.h>
int main()
{
int n, i, j, sum = 0;
printf("Enter the number of rows of square matrix: ");
scanf("%d", &n);
int a[n][n];
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf("Enter element (%d, %d) of matrices A: ", i+1, j+1);
scanf("%d", &a[i][j]);
}
}
printf("The matrix you entered is.....\n");
for(i = 0; i < n; i++){
for(j = 0; j < n; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(i+j == n-1 || i == j)
sum += a[i][j];
}
}
printf("Sum of diagonal elements = %d", sum);
return 0;
}
Comments
Post a Comment