Pages

Tuesday, 26 January 2016

Matrix addition


# To do addition in matrices # First change your font type as Courier
 m=int(raw_input("Enter no of rows : "))
n=int(raw_input("Enter no of columns : "))
a=[]
b=[]
c=[]
print " Matrix 1 "
for i in range(m):
    x=[]
    for j in range(n):
        y=int(raw_input("Enter element : "))
        x.append(y)
    a.append(x)
print " Matxix 2 "
for i in range(m):
    x=[]
    for j in range(n):
        y=int(raw_input("Enter element : "))
        x.append(y)
    b.append(x)
for i in range(m):
    x=[]
    for j in range(n):
        y=a[i][j]+b[i][j]
        x.append(y)
    c.append(x)
print " Matrix 1 "
for i in range(m):
    for j in range(n):
        print a[i][j],
    print
print " Matrix 2 "
for i in range(m):
    for j in range(n):
        print b[i][j],
    print
print " Matrix 1 + Matrix 2 "
for i in range(m):
    for j in range(n):
        print c[i][j],
    print


Output


Enter no of rows : 2
Enter no of columns : 2
 Matrix 1
Enter element : 1
Enter element : 1
Enter element : 1
Enter element : 1
 Matxix 2
Enter element : 2
Enter element : 2
Enter element : 2
Enter element : 2
 Matrix 1
1 1
1 1
 Matrix 2
2 2
2 2
 Matrix 1 + Matrix 2
3 3
3 3

Vv