Explanatory Question
Wap to Sum of diagonal elements of a matrix in c?
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.
#include int main() { int a[10][10], i, j, sum = 0, m, n; printf("Enter the row and column of matrix: "); scanf("%d %d", &m, &n); // Check for square matrix if (m != n) { printf("Diagonal elements exist only for square matrix.\n"); return 0; } printf("Enter the elements of matrix:\n"); 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++) { for (j = 0; j < n; j++) { printf("%d\t", a[i][j]); } printf("\n"); } // Sum of diagonal elements for (i = 0; i < m; i++) { sum = sum + a[i][i]; } printf("\nSum of the diagonal elements of the matrix is: %d", sum); return 0; }
Enter the row and column of matrix: 3 3 Enter the elements of matrix: 2 3 5 6 7 9 2 6 7 The matrix is 2 3 5 6 7 9 2 6 7 Sum of the diagonal elements of a matrix is: 16
Explanation:

Diagonal elements have been shown in the bold letter. We can observer the properties any element A ij will diagonal element if and only if i = j
First read the answer fully, then try to explain it in your own words. After that, open a few related questions and compare the concepts. This method helps you remember the topic for a longer time and improves exam preparation.