Answer to Question #283325 in Python for Kumar

Question #283325

given an n*n matrix write a program to print the sum of all biased elements an element biased if either or both of the following conditions is satisfied

1
Expert's answer
2021-12-28T12:59:48-0500
# Python 3.9.5

n = int(input('Enter rank of matrix: '))
a = []
for i in range(n):
    a.append(list(map(int, input().split())))
sum_first_diagonal = sum(a[i][i] for i in range(n))
sum_second_diagonal = 0
for i in range(n):
    for j in range(n):
        if i + j == n - 1:
            sum_second_diagonal += a[i][j]
if n % 2 == 0:
    sum = sum_first_diagonal + sum_second_diagonal
else:
    sum = sum_first_diagonal + sum_second_diagonal - a[int((n - 1)
                                                        / 2)][int((n - 1) / 2)]
print('Result:', sum)

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