Computer Science, asked by siddhantpathak165, 9 months ago

write a function which accept 2-D list of 3×3 and print the sum of both diagonals ?(PYTHON)​

Answers

Answered by sushiladevi4418
0

Answer:

A function that accepts the 2-D list of 3×3 and prints the sum of both diagonals.

Explanation:

# A simple Python program to

# find the sum of diagonals

MAX = 100

def printDiagonalSums(mat, n):

principal = 0

secondary = 0;

for i in range(0, n):

for j in range(0, n):

# Condition for principal diagonal

if (i == j):

principal += mat[i][j]

# Condition for secondary diagonal

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

secondary += mat[i][j]

print("Principal Diagonal:", principal)

print("Secondary Diagonal:", secondary)

# Driver code

a = [[ 1, 2, 3 ],

[ 5, 6, 7 ],

[ 1, 2, 3 ]]

printDiagonalSums(a, 3)

Output:

Principal Diagonal: 10

Secondary Diagonal: 10

Similar questions