Matrix Addition

# Take input for matrix dimensions
m = int(input("Enter number of rows: "))
n = int(input("Enter number of columns: "))

# Input first matrix
print("Enter elements for first matrix row-wise:")
A = []
for i in range(m):
    row = []
    for j in range(n):
        row.append(int(input()))  # Taking integer input manually
    A.append(row)

# Input second matrix
print("Enter elements for second matrix row-wise:")
B = []
for i in range(m):
    row = []
    for j in range(n):
        row.append(int(input()))  # Taking integer input manually
    B.append(row)

# Initialize result matrix
result = []
for i in range(m):
    row = []
    for j in range(n):
        row.append(A[i][j] + B[i][j])  # Add corresponding elements
    result.append(row)
print(result)
# Print result matrix
print("Resultant Matrix after Addition:")
for row in result:
    for num in row:
        print(num, end=" ")
    print()

Leave a Reply

Your email address will not be published. Required fields are marked *