134. Python Program to find Trace of the given matrix.
How does this program work?
- In this program we are going to learn about how to find Trace of the given matrix using Python.
- Trace of the matrix means Sum of diagonal elements of the given matrix.
- And then by using the formula we can find the trace of a matrix.
Here is the code
a = []
n=int(input("Enter the size of the matrix: "))
sum_1_diagonal = 0
sum_2_diagonal = 0
print("Enter the elements: ")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
a.append(row)
print(a)
print("Display an array In Matrix Form: ")
for i in range(n):
for j in range(n):
print(a[i][j], end=" ")
print()
for x in range(0, n):
for y in range(0, n):
if x == y:
sum_1_diagonal = sum_1_diagonal + a[x][y]
for x in range(0, n):
for y in range(0, n):
if x + y == n-1:
sum_2_diagonal = sum_2_diagonal + a[x][y]
print("Sum of right trace of matrix: ",sum_1_diagonal)
print( "Sum of left trace of matrix: ",sum_2_diagonal)