Python Program to Perform Addition,Subtraction and Multiplication of two Matrices














































Python Program to Perform Addition,Subtraction and Multiplication of two Matrices



Program to Perform Addition,Subtraction and Multiplication of two Matrices

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:2
Enter no. of columns for Matrix 1:2
Enter Elements of Matrix A
Enter element A00:1
Enter element A01:2
Enter element A10:3
Enter element A11:4
Enter no. of rows for Matrix 2:2
Enter no. of columns for Matrix 2:2
Enter Elements of Matrix B
Enter element B00:1
Enter element B01:2
Enter element B10:3
Enter element B11:4
1.Add Matrices
2.Subtract Matrices
3.Multiply Matrices
4.Exit
Enter Choice:1
Matrices can be Added
Resultant Matrix is:


 2  4 

 6  8 
Do you want to Continue?(press 0 to stop)a
1.Add Matrices
2.Subtract Matrices
3.Multiply Matrices
4.Exit
Enter Choice:2
Matrices can be Subtracted
Resultant Matrix is:


 0  0 

 0  0 
Do you want to Continue?(press 0 to stop)d
1.Add Matrices
2.Subtract Matrices
3.Multiply Matrices
4.Exit
Enter Choice:3
Matrices can be Multiplied
Resultant Matrix is:


 7  10 

 15  22 
Do you want to Continue?(press 0 to stop)0

Process finished with exit code 0


More Articles of abhijeet singh:

Name Views Likes
Python program to find diameter of a binary tree 820 16
Python program to find sum of all leaf nodes of binary tree 357 18
Python program to find height of a tree using recursion 385 16
Python program to find sum of all nodes in a binary tree 385 15
Python program to check whether a given binary tree is complete or not using recursion 322 18
Python program to check whether a binary tree is a full binary tree or not using recursion 428 30
Python program to convert a given binary search tree to doubly linked list 356 20
Python program to find K%u2019th largest element in binary search tree 342 25
Python program to find lowest Common Ancestor in a binary search tree 368 21
Python program to find k-th smallest element in binary search tree 324 17
Python program to find the node having maximum value in a binary search tree 327 19
Python program to find the node having minimum value in a binary search tree 385 18
Python program to construct binary search tree from given preorder traversal 377 18
Python Program to Implement Queue using two Stacks 404 19
Python Program to count and display occurence of a given Character and lines not starting with given Character 350 18
Python Program to find number of alphabets and number of lines in a file 303 17
Python Program to write numbers 1 to 100 in a file 327 17
Python Program to find number of words in the file 299 12
Python Program to create a file,find size of a file and copy content of one file in another 335 22
Python Program to create and delete a File 350 21
Python Program to demonstrate Monkey Patching 317 11
Python Program to find Sum of list (with string types) 344 21
Python program to create a list of tuples from given list having number and its cube in each tuple 435 18
Python Program to find missing number in a given integer array of 1 to 100 477 23
Python Program to perform files compression to tar file and extract a tar file 298 14
Python Program to demonstrate Various list Functions 444 15
Python GUI Registration Form Using Tkinter 915 25
Python Automated tool to convert Plain Text to Formatted text using Pygments 413 21
Python Program to Generate random numbers 327 20
Python Program to Swap two strings 376 20
Python Program to Perform Set Operations 354 22
Python Program to Find the Transpose of a Given Matrix 385 18
Python Program to Perform Addition,Subtraction and Multiplication of two Matrices 2192 23
Python Program to Calculate Permutation(nPr) and Combination(nCr) 712 23
Python Program to Find the Factorial of a Given Number 297 13
Python Program to Print the Fibonacci Sequence ( without Recursion) 367 26
Python Program to Calculate the Perimeter of a Square 373 28

Comments

  • Vivek
    21-Dec-2018 08:22:01 PM
    you can make it sort abhijeet also mention the last function
    • abhijeet
      21-Dec-2018 09:24:52 PM
      You know it's actually consisting of 3 programs like addition,subtraction and multiplication...i have used class and its object...Functions...checked for the conditions and made the program menu driven that's why its looking bit large...but i think that's reasonable...