Editor

Python Program to Find the Sum of a Matrix Diagonal

Beginner · Matrices

A square matrix has two diagonals. The main (principal) diagonal runs from the top-left to the bottom-right, and the secondary diagonal runs from the top-right to the bottom-left. This program prints a 4 x 4 matrix, lists the entries on each diagonal and adds them up.

Program

matrix_diagonal_sum.py
matrix = [[3, 8, 1, 6],
[5, 2, 9, 4],
[7, 0, 4, 2],
[1, 6, 3, 9]]
n = len(matrix) # a square matrix: n rows and n columns
print("Matrix:")
for row in matrix:
for value in row:
print(f"{value:3}", end="")
print()
main_items = []
secondary_items = []
for i in range(n):
main_items.append(matrix[i][i]) # row i, column i
secondary_items.append(matrix[i][n - 1 - i]) # row i, column n - 1 - i
print("Main diagonal:", main_items, "sum =", sum(main_items))
print("Secondary diagonal:", secondary_items, "sum =", sum(secondary_items))
Output
Matrix:
  3  8  1  6
  5  2  9  4
  7  0  4  2
  1  6  3  9
Main diagonal: [3, 2, 4, 9] sum = 18
Secondary diagonal: [6, 9, 0, 1] sum = 16

How it works

  1. On the main diagonal the row and column numbers are equal, so its entries are matrix[i][i].
  2. On the secondary diagonal the column counts down as the row counts up, so its entries are matrix[i][n - 1 - i].
  3. One loop over i collects both diagonals, because each row has exactly one entry on each.
  4. The main diagonal is 3, 2, 4, 9, which adds up to 18, and the secondary diagonal is 6, 9, 0, 1, which adds up to 16.

Other ways to write it

Using i == j and i + j == n - 1

Using i == j and i + j == n - 1
matrix = [[3, 8, 1, 6],
[5, 2, 9, 4],
[7, 0, 4, 2],
[1, 6, 3, 9]]
n = len(matrix)
main_sum = 0
secondary_sum = 0
for i in range(n):
for j in range(n):
if i == j:
main_sum += matrix[i][j]
if i + j == n - 1:
secondary_sum += matrix[i][j]
print("Main diagonal sum:", main_sum)
print("Secondary diagonal sum:", secondary_sum)
Output
Main diagonal sum: 18
Secondary diagonal sum: 16

The textbook method visits every entry and tests its position: i == j for the main diagonal and i + j == n - 1 for the secondary one.

Both diagonals together, centre counted once

Both diagonals together, centre counted once
matrix = [[4, 1, 2],
[7, 5, 3],
[8, 6, 9]]
n = len(matrix)
main_sum = sum(matrix[i][i] for i in range(n))
secondary_sum = sum(matrix[i][n - 1 - i] for i in range(n))
total = main_sum + secondary_sum
if n % 2 == 1:
total -= matrix[n // 2][n // 2] # the centre is on both diagonals
print("Main:", main_sum, "| Secondary:", secondary_sum, "| Both:", total)
Output
Main: 18 | Secondary: 15 | Both: 28

In a matrix with an odd number of rows, the centre entry lies on both diagonals, so it is subtracted once to avoid counting it twice.

Try it yourself

Change the program to print the sum of the entries above the main diagonal, where j > i.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises