forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python code to multiply two matrices
43 lines (42 loc) · 1.34 KB
/
Python code to multiply two matrices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#https://www.facebook.com/permalink.php?story_fbid=2376635879298242&id=100008555587566
#Subscribed and liked ny Aryan Kumar
# Program to multiply two matrices
A=[]
n=int(input("Enter N for N x N matrix: "))
print("Enter the element ::>")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
A.append(row)
print(A)
print("Display Array In Matrix Form")
for i in range(n):
for j in range(n):
print(A[i][j], end=" ")
print()
B=[]
n=int(input("Enter N for N x N matrix : "))
#use list for storing 2D array
#get the user input and store it in list (here IN : 1 to 9)
print("Enter the element ::>")
for i in range (n):
row=[]
for j in range(n):
row.append(int(input()))
B.append(row)
print(B)
#Display the 2D array
print("Display Array In Matrix Form")
for i in range(n):
for j in range(n):
print(B[i][j], end=" ")
print()
result = [[0,0,0], [0,0,0], [0,0,0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
print("The Resultant Matrix Is ::>")
for r in result:
print(r)