-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddition_mat.py
64 lines (62 loc) · 1.16 KB
/
addition_mat.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
r1=int(input("enter the number of rows:"))
c1=int(input("enter the number of columns:"))
matrix1=[]
print("enter the elements of a matrix:")
for i in range(r1):
a=[]
for j in range(c1):
a.append(int(input()))
matrix1.append(a)
for i in range(r1):
for j in range(c1):
print(matrix1[i][j], end=" ")
print()
r2=int(input("enter the number of rows:"))
c2=int(input("enter the number of columns:"))
matrix2=[]
print("enter the elements of a matrix:")
for i in range(r2):
b=[]
for j in range(c2):
b.append(int(input()))
matrix2.append(b)
for i in range(r2):
for j in range(c2):
print(matrix2[i][j], end=" ")
print()
print("Resultant matrix:")
res=[]
if r1==r2 and c1==c2:
for i in range(r1):
c=[]
for j in range(c1):
c.append(matrix1[i][j]+matrix2[i][j])
res.append(c)
else:
print("Invalid matrix.")
for i in range(r1):
for j in range(c1):
print(res[i][j] ,end=" ")
print()
output:
enter the number of rows:2
enter the number of columns:2
enter the elements of a matrix:
1
2
3
4
1 2
3 4
enter the number of rows:2
enter the number of columns:2
enter the elements of a matrix:
1
2
3
4
1 2
3 4
Resultant matrix:
2 4
6 8