---
title: "Sum of both diagonals of matrix"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:sum-both-diagonals"
source: https://www.bctengineer.com/q/semester_1/computer_programming/sum-both-diagonals
site: BCT Engineering
---

# Sum of both diagonals of matrix

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2074 Asoj, 4 marks
- Video: [C Program to find the Sum of Diagonal Elements of the Matrix](https://www.youtube.com/watch?v=GUn8ru-esNs) — Vaibhav Talks - Coding
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/sum-both-diagonals
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a program to read a square matrix of size N×N and find sum of both diagonals.

## Short answer

For an N×N matrix, principal diagonal elements have i = j, secondary diagonal i + j = N-1. Sum both in one loop: add a[i][i] to d1 and a[i][N-1-i] to d2. For matrix 1 2 3 / 4 5 6 / 7 8 9, both sums are 15.

## Answer

For an N×N matrix a[i][j]:
- Principal (main) diagonal: elements with i = j, that is a[i][i].
- Secondary diagonal: elements with i + j = N − 1, that is a[i][N−1−i].

One loop from 0 to N−1 adds both.

```c
#include <stdio.h>

int main(void)
{
    int a[10][10], n, i, j, d1 = 0, d2 = 0;
    printf("Enter order N of the square matrix: ");
    scanf("%d", &n);
    printf("Enter %d elements:\n", n * n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            scanf("%d", &a[i][j]);

    for (i = 0; i < n; i++) {
        d1 += a[i][i];            /* principal diagonal */
        d2 += a[i][n - 1 - i];    /* secondary diagonal */
    }
    printf("Sum of principal diagonal = %d\n", d1);
    printf("Sum of secondary diagonal = %d\n", d2);
    return 0;
}
```

**Sample run** (matrix 1 2 3 / 4 5 6 / 7 8 9)

Input: `3 1 2 3 4 5 6 7 8 9`

```text
Enter order N of the square matrix: Enter 9 elements:
Sum of principal diagonal = 15
Sum of secondary diagonal = 15
```

Main diagonal: 1 + 5 + 9 = 15; secondary diagonal: 3 + 5 + 7 = 15. (The centre element is counted in both sums when N is odd.)

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which elements form the principal diagonal?
   A. a[i][i]
   B. a[i][N-1-i]
   C. a[0][i]
   D. a[i][0]
   Answer: A. On the principal diagonal row index equals column index. (Key point: Principal diagonal)

2. Which condition identifies the secondary diagonal?
   A. i = j
   B. i - j = N
   C. i + j = N - 1
   D. i + j = N
   Answer: C. The secondary element in row i is in column N-1-i. (Key point: Secondary diagonal)

3. How many loops are needed to add both diagonals?
   A. One loop from 0 to N-1
   B. Two nested loops
   C. N loops of N
   D. Two separate loops going to N^2
   Answer: A. Both a[i][i] and a[i][N-1-i] are added in a single pass. (Key point: One loop)

4. For matrix 1 2 3 / 4 5 6 / 7 8 9, the two sums are
   A. 15 and 12
   B. 12 and 15
   C. 15 and 15
   D. 45 and 15
   Answer: C. 1+5+9 = 15 and 3+5+7 = 15; the centre element is in both. (Key point: Sample result)

## Related in Computer Programming

- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Sum and average between M and N using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-sum-average-range) — Computer Programming, chapter 9
- [Sum of two matrices in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-sum) — Computer Programming, chapter 11
- [Transpose m×n matrix in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-transpose) — Computer Programming, chapter 11
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Square footing size in clay, 750 kN](https://www.bctengineer.com/question.md?key=semester_5%2Ffoundation_engineering%3Asquare-footing-clay-750kn) — Foundation Engineering, chapter 6
- [Truss: flexibility matrix method](https://www.bctengineer.com/question.md?key=semester_5%2Ftheory_of_structures_ii%3Atruss-flexibility-matrix-100kn-50kn-4x4) — Theory of Structures II, chapter 2
- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
