generate functionWed, 08 Feb 2023

The trace of a square matrix is the sum of the elements of the main diagonal. Write a program that prints the trace of a given square matrix. Input data format The input to the program is a natural number. n is the number of rows and columns in the matrix, then the matrix elements (integers) line by line, separated by a space.

mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] r = 0 for c in range(len(mat)): r += mat[c][c] print(r)

Questions about programming?Chat with your personal AI assistant