Programmin-in-Python

Addition of 2 Matrices

Dec 21st, 2020 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. def main() :
  2.     row = int(input("Enter the number of rows in both the matrices : "))
  3.     col = int(input("Enter the number of column in both the matrices : "))
  4.    
  5.     M1 , M2 , sum_mat = [],[],[]
  6.    
  7.     for i in range(2) :    
  8.         for j in range(row) :
  9.             temp = []
  10.             for k in range(col) :
  11.                 if i == 0 :
  12.                     elem = eval(input("Enter the Element of the First Matrix at the Position Row : {} , Column : {} =====> ".format(j+1,k+1)))
  13.                 else : elem = eval(input("Enter the Element of the Second Matrix at the Position Row : {} , Column : {} =====> ".format(j+1,k+1)))
  14.                 temp.append(elem)
  15.             if i == 0 : M1.append(temp)
  16.             else : M2.append(temp)
  17.     print('\nMatrix A : ')
  18.     for l in M1 :
  19.         for o in l :
  20.             print(o , end = "  ")
  21.         print()
  22.     print("\nMatrix B : ")
  23.     for m in M2 :
  24.         for n in m :
  25.             print(n , end = "  ")
  26.         print()
  27.    
  28.     temp = []
  29.     for i in range(row) :
  30.         for j in range(col) :
  31.             sum_elem = M1[i][j] + M2[i][j]
  32.             temp.append(sum_elem)
  33.         sum_mat.append(temp)
  34.         temp = []
  35.    
  36.     print("\nSum Of The Matrices : ")
  37.     for i in sum_mat :
  38.         for j in i :
  39.             print(j , end = "  ")
  40.         print()
  41.    
  42. main()
Add Comment
Please, Sign In to add comment