Answer to Question #290231 in Python for pooja

Question #290231

given a square matrix print the sum all diagonal elements of the matrix



sample input :


3


1 2 3


4 5 6


7 8 9


sample output:


25

1
Expert's answer
2022-01-24T09:03:36-0500
N = int(input())
matrix = []
for i in range(N):
    matrix.append([int(j) for j in input().split()])
diagonalSum = 0
antiDiagonalSum = 0
for i in range(N):
    diagonalSum += matrix[i][i]
    antiDiagonalSum += matrix[i][N - 1 - i]


if N % 2 != 0:
    antiDiagonalSum -= matrix[N // 2][N // 2]


print(diagonalSum + antiDiagonalSum)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS