Print the sum of diagonal elements of a matrix. Take input from the user.

Asked: Anonymously |Category: Python

n1=input(" Enter the dimension of matrix: ")

n1=int(n1)

n=[]

l=[0]*n1

d=0

da=0

for i in range(0,n1):

new=[]

for j in range(0,n1):

new.append(0)

n.append(new)

for i in range(0,n1):

l=[int(i) for i in input().split(" ")]

for j in range(0,n1):

n[i][j]=l[j]

for i in range(0,n1):

for j in range(0,n1):

if(i==j):

d=d+n[i][j]

if((i+j)==(n1-1)):

da=da+n[i][j]

print(d)

print(da)

4 views

Recent Posts

See All

"Believe you can and you're halfway there"