C Program to Find Sum of Diagonal Elements
sum += matrix[i][i]; inside a loop from 0 to n-1.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> int main() { int n, sum = 0; printf("Enter size of square matrix: "); scanf("%d", &n); int matrix[n][n]; printf("Enter matrix elements:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matrix[i][j]); } } for (int i = 0; i < n; i++) { sum += matrix[i][i]; } printf("Sum of diagonal elements: %d\n", sum); return 0; }
Dry Run
Let's trace the example with a 3x3 matrix [[1,2,3],[4,5,6],[7,8,9]] through the code.
Input matrix size
User inputs n = 3
Input matrix elements
User inputs matrix elements row-wise: 1 2 3, 4 5 6, 7 8 9
Initialize sum
sum = 0
Add diagonal elements
sum = 0 + matrix[0][0] = 0 + 1 = 1 sum = 1 + matrix[1][1] = 1 + 5 = 6 sum = 6 + matrix[2][2] = 6 + 9 = 15
Print result
Output: Sum of diagonal elements: 15
| Iteration | i | matrix[i][i] | sum |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
| 2 | 1 | 5 | 6 |
| 3 | 2 | 9 | 15 |
Why This Works
Step 1: Matrix input
We first get the size and elements of the square matrix from the user to work with actual data.
Step 2: Sum initialization
We start with sum = 0 to accumulate the diagonal elements without leftover values.
Step 3: Diagonal addition
We add elements where row and column indices are equal using sum += matrix[i][i]; to get the diagonal sum.
Step 4: Output
Finally, we print the sum to show the result to the user.
Alternative Approaches
#include <stdio.h> int main() { int n, sum = 0; printf("Enter size of square matrix: "); scanf("%d", &n); int matrix[n][n]; printf("Enter matrix elements:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matrix[i][j]); } } for (int i = 0; i < n; i++) { sum += matrix[i][i]; // primary diagonal sum += matrix[i][n - i - 1]; // secondary diagonal } if (n % 2 == 1) { sum -= matrix[n/2][n/2]; // subtract middle element once if odd size } printf("Sum of both diagonals: %d\n", sum); return 0; }
#include <stdio.h> int main() { int n, sum = 0; printf("Enter size of square matrix: "); scanf("%d", &n); int matrix[n][n]; printf("Enter matrix elements:\n"); for (int i = 0; i < n * n; i++) { scanf("%d", ((int *)matrix) + i); } for (int i = 0; i < n; i++) { sum += *(((int *)matrix) + i * (n + 1)); } printf("Sum of diagonal elements: %d\n", sum); return 0; }
Complexity: O(n^2) time, O(n^2) space
Time Complexity
Reading the matrix requires looping through all n*n elements, which is O(n^2). Summing diagonal elements is O(n), but dominated by input reading.
Space Complexity
The matrix is stored in a 2D array of size n*n, so space complexity is O(n^2). The sum variable uses O(1) space.
Which Approach is Fastest?
All approaches require reading the full matrix, so time is similar. Pointer arithmetic may be slightly faster but less readable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple loop with indices | O(n^2) | O(n^2) | Readability and beginners |
| Sum both diagonals | O(n^2) | O(n^2) | When both diagonals needed |
| Pointer arithmetic | O(n^2) | O(n^2) | Efficiency and advanced users |
matrix[i][i] to access them.