List - Matrix Addition

# Matrix Addition

Mat_A = []
Mat_B = []
Mat_Add = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Addition
if ( (r1 != r2) or (c1 != c2) ):
    print("Rows and columns of given matrices do not match.")
    print("Cannot Add the matrices.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)
   
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = Mat_A[i][j] + Mat_B[i][j]
            tmp.append(ele)
        Mat_Add.append(tmp)

    print("Sum of given matrices : ")
   
    for row in Mat_Add:
        for ele in row:
            print(ele, end=" ")
        print()
   

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu