1) Given Matrices can be added if no. of Rows and Columns of matrix 1 is equal to no. of Rows and Columns of matrix 2 respectively,2) Given Matrices can be Subtracted if no. of Rows and Columns of matrix 1 is equal to no. of Rows and Columns of matrix 2 respectively,3)Given Matrices can be multiplied if no. of Columns of matrix 1 is equal to no. of Rows for matrix 2

Program:

class Matrices:def __init__(self,A,B):`self.A=A`

`self.B=B`

def display(self,C):print('Resultant Matrix is:')for i in range(0,m):print('\n')for j in range(0,n):print(' {}'.format(C[i][j]),end=" ")def Add(self,C):for i in range(0,m):for j in range(0,n):C[i][j] = A[i][j] + B[i][j]def Sub(self,C):for i in range(0,m):for j in range(0,n):C[i][j] = A[i][j] - B[i][j]def Mul(self,C):for i in range(0,m):for j in range(0,q):for k in range(0,n):C[i][j]+= A[i][k] * B[k][j]if __name__=='__main__':m = int(input('Enter no. of rows for Matrix 1:'))n = int(input('Enter no. of columns for Matrix 1:'))A = [[0 for j in range(0, n)] for i in range(0, m)]print('Enter Elements of Matrix A')for i in range(0, m):for j in range(0, n):A[i][j] = int(input('Enter element A{}{}:'.format(i, j)))p = int(input('Enter no. of rows for Matrix 2:'))q = int(input('Enter no. of columns for Matrix 2:'))B = [[0 for j in range(0, q)] for i in range(0, p)]print('Enter Elements of Matrix B')for i in range(0, p):for j in range(0, q):B[i][j] = int(input('Enter element B{}{}:'.format(i, j)))obj = Matrices(A,B)`var =1`

while var!='0':print('1.Add Matrices\n2.Subtract Matrices\n3.Multiply Matrices\n4.Exit')choice = int(input('Enter Choice:'))if choice==1:if m==p and n==q:print('Matrices can be Added')C = [[0 for j in range(0, n)] for i in range(0, m)]obj.Add(C)obj.display(C)`else:`

print('Matrices cannot be Added')elif choice==2:if m==p and n==q:print('Matrices can be Subtracted')C = [[0 for j in range(0, n)] for i in range(0, m)]obj.Sub(C)obj.display(C)`else:`

print('Matrices cannot be Subtracted')elif choice==3:`if n==p:`

print('Matrices can be Multiplied')C = [[0 for j in range(0, q)] for i in range(0, m)]obj.Mul(C)obj.display(C)`else:`

print('Matrices cannot be Multiplied')elif choice==4:exit(0)`else:`

print('\nPlease enter a valid choice')var = (input('\nDo you want to Continue?(press 0 to stop)'))INPUT & OUTPUT:

Enter no. of rows for Matrix 1:2Enter no. of columns for Matrix 1:2Enter Elements of Matrix AEnter element A00:1Enter element A01:2Enter element A10:3Enter element A11:4Enter no. of rows for Matrix 2:2Enter no. of columns for Matrix 2:2Enter Elements of Matrix BEnter element B00:1Enter element B01:2Enter element B10:3Enter element B11:41.Add Matrices2.Subtract Matrices3.Multiply Matrices4.ExitEnter Choice:1Matrices can be AddedResultant Matrix is:2 46 8Do you want to Continue?(press 0 to stop)a1.Add Matrices2.Subtract Matrices3.Multiply Matrices4.ExitEnter Choice:2Matrices can be SubtractedResultant Matrix is:0 00 0Do you want to Continue?(press 0 to stop)d1.Add Matrices2.Subtract Matrices3.Multiply Matrices4.ExitEnter Choice:3Matrices can be MultipliedResultant Matrix is:7 1015 22Do you want to Continue?(press 0 to stop)0Process finished with exit code 0

## Comments

## Vivek

21-Dec-2018 08:22:01 PM## abhijeet

21-Dec-2018 09:24:52 PM