forked from namishkhanna/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_operations.py
67 lines (52 loc) · 1.76 KB
/
Matrix_operations.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#Python program for matrix operations- addition and multiplication
matrix = []
No_of_rows = int(input("Enter the number of rows: "))
No_of_Columns = int(input("Enter the number of Columns: "))
for i in range(No_of_rows):
matrix.append([])
for j in range(No_of_Columns):
value = int(input("Enter the element: "))
matrix[i].append(value)
print("The 2D matrix is: ", matrix)
print("MATRIX 1")
for i in range(No_of_rows):
for j in range(No_of_Columns):
print(matrix[i][j], end=" ") #new line
print()
matrix2 = []
No_of_rows = int(input("Enter the number of rows: "))
No_of_Columns = int(input("Enter the number of Columns: "))
for i in range(No_of_rows):
matrix2.append([])
for j in range(No_of_Columns):
value = int(input("Enter the element: "))
matrix2[i].append(value)
print("The 2D matrix is: ", matrix2)
print("MATRIX 2")
for i in range(No_of_rows):
for j in range(No_of_Columns):
print(matrix2[i][j], end=" ") #new line
print()
#To Add M1 and M2 matrices
result = [[0,0,0], [0,0,0], [0,0,0]]
for i in range(len(matrix)):
for k in range(len(matrix2)):
result[i][k] = matrix[i][k] + matrix2[i][k]
#To Print the matrix
print("The sum of Matrix M1 and M2 = ", result)
print("RESULTANT ADDITION")
for i in range(No_of_rows):
for j in range(No_of_Columns):
print(result[i][j], end=" ") #new line
print()
#To Multiply M1 and M2 matrices
for i in range(len(matrix)):
for k in range(len(matrix2)):
result[i][k] = matrix[i][k] * matrix2[i][k]
#To Print the matrix
print("The multiplication of Matrix M1 and M2 = ", result)
print("RESULTANT MULTIPLICATION")
for i in range(No_of_rows):
for j in range(No_of_Columns):
print(result[i][j], end=" ") #new line
print()