Q18. Write a code to add two matrices

Solution :In this program, you’ll learn to add two matrices using Nested loop and display it using Nested loop.

Matrices:- A two dimensional(2D) array is known as Matrix. It can have m number of rows and n number of columns where m and n are two integer values.

Algorithm for Matrix Addition

//Algorithm to Add Two Matrices.
START
Step 1 -> Input matrix 1 and matrix 2.
Step 2 -> If the number of rows and number of columns of matrix 1 and matrix 2 are equal then execute step 3 else addition not possible
Step 3 -> for i=1 to rows[matrix 1]
for j=1 to columns [matrix 1]
Input matrix 1 [i,j]
Input matrix 2 [i,j]
matrix 3 [i,j]= matrix 1 [i,j] + matrix 2 [i,j];
step 4-> Display matrix 3 [i,j];
STOP

Code  for Matrix Addition

Output

mat1  mat2
1          2
3          4
5          6
7          8
sum matrix :
3        7
11     15

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number