Python Program To Add Two Matrices Taking Input From User (2 Ways)

Python Program To Add Two Matrices Taking Input From User

In this tutorial, you will learn to write a Python Program To Add Two Matrices Taking Input From User.

NumPy is a Python library used for scientific computing and data analysis.

It provides support for multidimensional arrays, matrices, and high-level mathematical functions to operate on these arrays.

NumPy is a powerful library for numerical computations in Python.

It is widely used in the fields of data science, machine learning, and artificial intelligence.

NumPy arrays are similar to Python lists, but they allow for fast, element-wise operations and efficient handling of large data sets.

To understand this program you should have an understanding of the following topics:

  1. Python Lists
  2. Numpy

1: Python Program To Add Two Matrices Taking Input From User (Using Lists)

# Take the dimensions of the matrices as input from the user
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))

# Take input for the first matrix
print("Enter elements of the first matrix:")
matrix1 = []
for i in range(rows):
    row = []
    for j in range(cols):
        row.append(float(input()))
    matrix1.append(row)

# Take input for the second matrix
print("Enter elements of the second matrix:")
matrix2 = []
for i in range(rows):
    row = []
    for j in range(cols):
        row.append(float(input()))
    matrix2.append(row)

# Add the two matrices
matrix_sum = []
for i in range(rows):
    row = []
    for j in range(cols):
        row.append(matrix1[i][j] + matrix2[i][j])
    matrix_sum.append(row)

# Print the result
print("The sum of the two matrices is:")
for i in range(rows):
    for j in range(cols):
        print(matrix_sum[i][j], end=" ")
    print()

Output

Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
3
4
5
6
Enter elements of the second matrix:
9
8
7
6
The sum of the two matrices is:
12.0 12.0
12.0 12.0

In this program, we first take the dimensions of the matrices as input from the user using the input function and convert them to integers using the int function.

We then use nested loops to take input for the elements of the two matrices.

First initialize an empty list called matrix1 and then use two nested loops to append the user input to this list.

We do the same for the second matrix, which we store in a list called matrix2.

Next, we use another set of nested loops to add the two matrices element-wise.

We initialize an empty list called matrix_sum and then use two nested loops to append the sum of the corresponding elements of the two matrices to this list.

Finally, we print out the sum of the two matrices by using two nested loops to iterate over the elements of the matrix_sum list and print them out in matrix form.

2: Python Program To Add Two Matrices Taking Input From User (Using Numpy)

import numpy as np

# Take the dimensions of the matrices as input from the user
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))

# Take input for the first matrix
print("Enter elements of the first matrix:")
matrix1 = np.zeros((rows, cols), dtype=int)
for i in range(rows):
    for j in range(cols):
        matrix1[i][j] = int(input())

# Take input for the second matrix
print("Enter elements of the second matrix:")
matrix2 = np.zeros((rows, cols), dtype=int)
for i in range(rows):
    for j in range(cols):
        matrix2[i][j] = int(input())

# Add the two matrices
matrix_sum = matrix1 + matrix2

# Print the result
print("Matrix 1:\n", matrix1)
print("Matrix 2:\n", matrix2)
print("The sum of the Matrix 1 and Matrix 2 is:")
print(matrix_sum)

Output

Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
4
5
6
7
Enter elements of the second matrix:
9
8
7
6
Matrix 1:
[[4 5]
[6 7]]
Matrix 2:
[[9 8]
[7 6]]
The sum of the Matrix 1 and Matrix 2 is:
[[13 13]
[13 13]]

In this program, we first import the NumPy library using the import statement.

We then take the dimensions of the matrices as input from the user using the input function and convert them to integers using the int function.

Then we used nested loops to take input for the elements of the two matrices.

We initialize two NumPy arrays of zeros using the np.zeros function and the input dimensions.

We then use two nested loops to assign the user input values to the elements of these arrays.

The int function is used to convert the input values to integers.

Next, we use the + operator to add the two matrices and store the result in a new NumPy array called matrix_sum.

Finally, we print out the sum of the two matrices using the print function and pass it into the matrix_sum array.

NumPy takes care of formatting the output in matrix form.


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading