Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Program for Addition of Two Matrices

The document provides Python programs for adding and multiplying two user-input matrices using nested loops. It outlines the algorithms for both operations and includes example code for each, demonstrating how to input matrices and display the results. The output examples illustrate the expected results for given inputs.

Uploaded by

rincejohn80
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Program for Addition of Two Matrices

The document provides Python programs for adding and multiplying two user-input matrices using nested loops. It outlines the algorithms for both operations and includes example code for each, demonstrating how to input matrices and display the results. The output examples illustrate the expected results for given inputs.

Uploaded by

rincejohn80
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Program for Addition of Two Matrices

Given two user input matrix. Our task is to display the addition of two matrix. In these
problem we use nested List comprehensive.

Algorithm
Step1: input two matrix.
Step 2: nested for loops only to iterate through each row and columns.
Step 3: At each iteration shall add the corresponding elements from two matrices and shall
store the result.

Example code
# Program to add two matrices using nested loop
A=[]
n=int(input("Enter N for N x N matrix : "))
print("Enter the element ::>")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
A.append(row)
print(A)
print("Display Array In Matrix Form")
for i in range(n):
for j in range(n):
print(A[i][j], end=" ")
print()
B=[]
n=int(input("Enter N for N x N matrix : "))
print("Enter the element ::>")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
B.append(row)
print(B)
print("Display Array In Matrix Form")
for i in range(n):
for j in range(n):
print(B[i][j], end=" ")
print()
result = [[0,0,0], [0,0,0], [0,0,0]]
for i in range(n):
for j in range(len(A[0])):
result[i][j] = A[i][j] + B[i][j]
print("Resultant Matrix is ::>")
for r in result:
print("Resultant Matrix is ::>",r)

Output

Enter N for N x N matrix : 3


Enter the element ::>
10
10
10
20
20
20
30
30
30
[[10, 10, 10], [20, 20, 20], [30, 30, 30]]
Display Array In Matrix Form
10 10 10
20 20 20
30 30 30
Enter N for N x N matrix : 3
Enter the element ::>
100
100
100
200
200
200
300
300
300
[[100, 100, 100], [200, 200, 200], [300, 300, 300]]
Display Array In Matrix Form
100 100 100
200 200 200
300 300 300
Resultant Matrix is ::> [110, 110, 110]
[220, 220, 220]
[330, 330, 330]

Python Program for Multiplication of Two Matrices


Given two user input matrix. Our task is to display the addition of two matrix. In these
problem we use nested List comprehensive.

Algorithm
Step1: input two matrix.
Step 2: nested for loops to iterate through each row and each column.
Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row
elements of first matrix with each elements of second matrix, then add all multiplied value.
That is the value of resultant matrix.
Example Code
A=[]
n=int(input("Enter N for N x N matrix: "))
print("Enter the element ::>")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
A.append(row)
print(A)
print("Display Array In Matrix Form")
for i in range(n):
for j in range(n):
print(A[i][j], end=" ")
print()
B=[]
n=int(input("Enter N for N x N matrix : "))
print("Enter the element ::>")
for i in range (n):
row=[]
for j in range(n):
row.append(int(input()))
B.append(row)
print(B)
print("Display Array In Matrix Form")
for i in range(n):
for j in range(n):
print(B[i][j], end=" ")
print()
result = [[0,0,0], [0,0,0], [0,0,0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
print("The Resultant Matrix Is ::>")
for r in result:
print(r)

Output
Enter N for N x N matrix: 3
Enter the element ::>
2
1
4
2
1
2
3
4
3
[[2, 1, 4], [2, 1, 2], [3, 4, 3]]
Display Array In Matrix Form
214
212
343
Enter N for N x N matrix : 3
Enter the element ::>
1
2
3
4
5
6
7
8
9
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Display Array In Matrix Form
123
456
789
The Resultant Matrix Is ::>
[34, 41, 48]
[20, 25, 30]
[40, 50, 60]

You might also like