Programming Examples

Write a program to perform the addition of two square matrices.


Write a program to perform the addition of two square matrices.

Solution

import numpy as np

# Initialize two square matrices
A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

B = np.array([[9, 8, 7],
              [6, 5, 4],
              [3, 2, 1]])

# Add the matrices
C = A + B

# Display the matrices
print("First Matrix:")
print(A)

print("\nSecond Matrix:")
print(B)

print("\nSum of the Matrices:")
print(C)
▶ RUN

Output/ Explanation:

Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad