In this post, you will learn the python program to multiply two matrices by taking input from the user but before writing a program let’s understand the rule for matrix multiplication and the algorithm to implement it.

## Rules for Matrix multiplication

Let’s understand the rules for matrix multiplication with help of an example:

Example:

## Algorithm for Matrix multiplication in Python

From the above algorithm, we understood how to implement the python program to multiply two matrices by taking input from the user.

But before writing a program few programming concepts you have to know:

## Source code

``````# matrix multiplication in python
matrix_A_rows = int(input('ENter number of rows for matrix-A: '))
matrix_A_cols  = int(input('ENter number of columns for matrix-A: '))

print() # for new line

matrix_B_rows = int(input('ENter number of rows for matrix-B: '))
matrix_B_cols  = int(input('ENter number of columns for matrix-B: '))

print() # for new line

if matrix_A_cols == matrix_B_rows:
print('Enter values for matrix A')
matrix_A = [[int(input(f"column {j+1} -> ENter {i+1} element:")) for j in range(matrix_A_cols)] for i in range(matrix_A_rows) ]

print() # for new line

print('Enter values for matrix B ')
matrix_B = [[int(input(f"column {j+1} -> ENter {i+1} element:")) for j in range(matrix_B_cols)] for i in range(matrix_B_rows) ]

print() #for new line

print('Matrix-A :')
for i in matrix_A:
print(i)

print() #for new line

print('Matrix-B :')
for i in matrix_B:
print(i)

# mutiplication operation

# resultant matrix (matrix that store answer and intially it is Zero)
result = [[0 for j in range(matrix_B_cols)] for i in range(matrix_A_rows)]

# main logic for matrix multiplication (multiplication operation)
for i in range(len(matrix_A)):
for j in range(len(matrix_B[0])):
for k in range(len(matrix_B)):
result[i][j] += matrix_A[i][k] * matrix_B[k][j]

print() #for new line

print('Multiplication of Matrix-A and Matrix-B is :')
for i in result:  #print result
print(i)

else:
print('Multiplication of matrices is not possible (columns of matrix-A = row of matrix-B)')
``````

## Output

``````ENter number of rows for matrix-A: 2
ENter number of columns for matrix-A: 3

ENter number of rows for matrix-B: 3
ENter number of columns for matrix-B: 2

Enter values for matrix A
column 1 -> ENter 1 element:1
column 2 -> ENter 1 element:2
column 3 -> ENter 1 element:3
column 1 -> ENter 2 element:4
column 2 -> ENter 2 element:5
column 3 -> ENter 2 element:6

Enter values for matrix B
column 1 -> ENter 1 element:10
column 2 -> ENter 1 element:11
column 1 -> ENter 2 element:20
column 2 -> ENter 2 element:21
column 1 -> ENter 3 element:30
column 2 -> ENter 3 element:31

Matrix-A :
[1, 2, 3]
[4, 5, 6]

Matrix-B :
[10, 11]
[20, 21]
[30, 31]

Multiplication of Matrix-A and Matrix-B is :
[140, 146]
[320, 335]``````

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.