Answer to Question #302855 in Python for Cherry

Question #302855

Sum of Non-Diagonals

Write a program to print the following,


Sample Input1

3

9 8 7

6 5 4

3 2 1

Sample Output1

20


1
Expert's answer
2022-03-02T15:34:50-0500
def sum_mat(arr,N):
    sum_mat1 = 0
    sum_mat2 = 0
    sum_mat3 = 0
    sum_mat4 = 0
    totalsum = 0
 
    for i in range(N):
        for j in range(N):
            
            if i + j < N - 1:
                 
                if i < j and i != j and i + j:
                    sum_mat1 += arr[i][j]
                
                elif i != j:
                    sum_mat2 += arr[i][j]
            else
                 
                if i > j and i + j != N - 1:
                    sum_mat3 += arr[i][j]
                else:
                    
                    if i + j != N - 1 and i != j:
                        sum_mat4 += arr[i][j]
    return sum_mat1 + sum_mat2 + sum_mat3 + sum_mat4

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