-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddMatrix.py
More file actions
29 lines (24 loc) · 735 Bytes
/
addMatrix.py
File metadata and controls
29 lines (24 loc) · 735 Bytes
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
#This is a program to add two matrices represented using Python Lists
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))
def matrix(name):
print(f"Enter elements for {name}:")
matrix = []
for i in range(rows):
row = []
for j in range(cols):
val = int(input(f"Enter element at row {i + 1}, column {j + 1}: "))
row.append(val)
matrix.append(row)
return matrix
matrix1 = matrix("Matrix 1")
matrix2 = matrix("Matrix 2")
result = []
for i in range(rows):
rrow = []
for j in range(cols):
rrow.append(matrix1[i][j] + matrix2[i][j])
result.append(rrow)
print("Resultant Matrix:")
for row in result:
print(row)