0% found this document useful (0 votes)
6 views7 pages

DSL PR 3

Uploaded by

tg467900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

DSL PR 3

Uploaded by

tg467900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical NO : 3

Write a python program to compute following computation on matrix:

a) Addition of two matrices

b) Subtraction of two matrices

c) Multiplication of two matrices

d) Transpose of a matrix

Code :-

print("*************************************************************************\n")

A=[]

n=int(input("Enter 'N' for N*N Matrix A :"))

print("Enter the elements")

for i in range(n):

row=[]

for j in range(n):

row.append(int(input()))

A.append(row)

print("Matrix A :")

for i in range(n):

for j in range(n):

print(A[i][j], end=" ")

print()

print("\n***********************************************************************\n")
B=[]

n=int(input("Enter 'N' for N*N Matrix B :"))

print("Enter the elements")

for i in range(n):

row=[]

for j in range(n):

row.append(int(input()))

B.append(row)

print("Matrix B :")

for i in range(n):

for j in range(n):

print(B[i][j], end=" ")

print()

result1= [[0,0,0],

[0,0,0],

[0,0,0]]

result2= [[0,0,0],

[0,0,0],

[0,0,0]]

result3= [[0,0,0],

[0,0,0],

[0,0,0]]
result4= [[0,0,0],

[0,0,0],

[0,0,0]]

result5= [[0,0,0],

[0,0,0],

[0,0,0]]

for i in range(len(A)):

for j in range(len(A[0])):

result1[i][j]=A[i][j]+B[i][j]

result2[i][i]=A[i][j]-B[i][j]

for i in range(len(A)):

for j in range(len(B[0])):

for k in range(len(B)):

result3[i][j]+=A[i][j]*B[i][j]

print("\n***********************************************************************"\n”)

for i in range(len(A)):

for j in range(len(A[0])):

result4[j][i]=A[i][j]

for i in range(len(B)):

for j in range(len(B[0])):

result5[j][i]=B[i][j]

print("\n***********************************************************************\n")

print("Addition of Matrices :")


for g in result1:

print(g)

print("\n***********************************************************************\n")

print("Substraction of Matrices :")

for g in result2:

print(g)

print("\n***********************************************************************\n")

print("Multiplication of Matrices :")

for g in result3:

print(g)

print("\n***********************************************************************\n")

print("Transpose of Matrix A :")

for g in result4:

print(g)

print("\n***********************************************************************\n")

print("Transpose of Matrix B :")

for g in result5:

print(g)

print("\n*************************************************************************")
OUTPUT :

*********************************************************************************

Enter 'N' for N*N Matrix A :3

Enter the elements

Matrix A :

1 2 3

4 5 6

7 8 9

**********************************************************************************

Enter 'N' for N*N Matrix B :3

Enter the elements

7
5

Matrix B :

8 9 7

5 4 6

3 2 5

**********************************************************************************

**********************************************************************************

Addition of Matrices :

[9, 11, 10]

[9, 9, 12]

[10, 10, 14]

**********************************************************************************

Substraction of Matrices :

[-4, 0, 0]

[0, 0, 0]

[0, 0, 4]

**********************************************************************************
Multiplication of Matrices :

[24, 54, 63]

[60, 60, 108]

[63, 48, 135]

**********************************************************************************

Transpose of Matrix A :

[1, 4, 7]

[2, 5, 8]

[3, 6, 9]

**********************************************************************************

Transpose of Matrix B :

[8, 5, 3]

[9, 4, 2]

[7, 6, 5]

**********************************************************************************

You might also like