AnkitWebLogic

Q. Write a Program to find sum of all rows, all columns and diagonals of a 3x3 matrix in Python. 1

#find sum of all Rows, Columns and Diagonals.
from numpy import *
r1=r2=r3=c1=c2=c3=0
r=-1
arr = array([[1,2,3], [4,5,6], [7,8,9]])
for i in arr:
    r=r+1
    c=-1
    for j in i:
        c=c+1
        if r==0:
            r1 = r1 + arr[r][c]
        if r==1:
            r2 = r2 + arr[r][c]
        if r==2:
            r3 = r3 + arr[r][c]
        if c==0:
            c1 = c1 + arr[r][c]
        if c==1:
            c2 = c2 + arr[r][c]
        if c==2:
            c3 = c3 + arr[r][c]
        print(j, end=' ')
    print()

print("Sum of 1st row is: ", r1)
print("Sum of 2nd row is: ", r2)
print("Sum of 3rd row is: ", r3)
print("Sum of 1st col is: ", c1)
print("Sum of 2nd col is: ", c2)
print("Sum of 3rd col is: ", c3)
Enter 3x3 matrix:
1
2
3
4
5
6
7
8
9
Sum of 1st row is: 6
Sum of 2nd row is: 15
Sum of 3rd row is: 24
Sum of 1st col is: 12
Sum of 2nd col is: 15
Sum of 3rd col is: 18

Ad: