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
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
Comments
Leave a comment