multi

# Input for the first matrix
rows1 = int(input("Enter the number of rows for the first matrix: "))
cols1 = int(input("Enter the number of columns for the first matrix: "))
matrix1 = []
print("Enter the elements of the first matrix row-wise:")
for i in range(rows1):
    row = []
    for j in range(cols1):
        element = float(input(f"Enter element at position ({i+1}, {j+1}): "))
        row.append(element)
    matrix1.append(row)

# Input for the second matrix
rows2 = int(input("Enter the number of rows for the second matrix: "))
cols2 = int(input("Enter the number of columns for the second matrix: "))
matrix2 = []
print("Enter the elements of the second matrix row-wise:")
for i in range(rows2):
    row = []
    for j in range(cols2):
        element = float(input(f"Enter element at position ({i+1}, {j+1}): "))
        row.append(element)
    matrix2.append(row)

# Check if matrices can be added
if rows1 != rows2 or cols1 != cols2:
    print("Error: Matrices must have the same dimensions to be added.")
else:
    # Add the matrices
    result = []
    for i in range(rows1):
        row = []
        for j in range(cols1):
            row.append(matrix1[i][j] + matrix2[i][j])
        result.append(row)

    # Display the result
    print("The sum of the matrices is:")
    for row in result:
        print(row)
Share on Google Plus

About THE CHOOSEN

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment