Q:

Write a Python program to read a square matrix from console and print the sum of matrix primary diagonal. Accept the size of the square matrix and elements for each column separated with a space (for every row) as input from the user

0

Write a Python program to read a square matrix from console and print the sum of matrix primary diagonal. Accept the size of the square matrix and elements for each column separated with a space (for every row) as input from the user

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

size = int(input("Input the size of the matrix: "))
matrix = [[0] * size for row in range(0, size)]
for x in range(0, size):

    line = list(map(int, input().split()))

    for y in range(0, size):
        matrix[x][y] = line[y]

matrix_sum_diagonal = sum(matrix[size - i - 1][size - i - 1] for i in range(size))
print("Sum of matrix primary diagonal:")
print(matrix_sum_diagonal)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now