Calculate the sum of the diagonal elements in a 3x3 matrix.
MediumProblem Statement
Calculate the sum of the diagonal elements in a 3x3 matrix.
Sample Input
Matrix: 1 2 3 4 5 6 7 8 9
Sample Output
Sum of diagonal elements: 15
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
#include <stdio.h>
int diagonalSum(int matrix[3][3]) {
// Your code here
}
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Sum of diagonal elements: %d\n", diagonalSum(matrix));
return 0;
}
Input
Matrix: 1 2 3 4 5 6 7 8 9
Expected Output
Sum of diagonal elements: 15