Problem: Write a program in C to add two matrices.

A matrix in C programming language is represented as a 2D array.

Therefore, to implement matrix addition in C, we need to add every element of the first array with its corresponding element of the second array and store the result into the same index position of the resultant array i.e. res[i][j] = matrix1[i][j] + matrix2[i][j].

But before proceeding for addition, we need to make sure that the order of both the matrices are same i.e.

  • The number of rows of the matrix I = Number of rows of Matrix II.
  • The number of columns of Matrix I = Number of columns of Matrix II.

If the order of the matrices are not same then matrix addition will not be possible.

Here is the example of matrix addition in c.

Output

When matrices have the same dimensions.

Matrix Addition in C

When the matrices have different dimensions.

Matrix Addition C program

In this tutorial, we learned to add two matrices in C programming language. If you have any doubt then do comment below.

Leave a Reply