Addition of two matrix in C

In this article we will make some C programs to add two matrix or subtract two matrix using C programming language. We will take two matrix as input from the user and them add them and print another matrix which will be the sum of first two matrix. Consider a user gives two matrix A and B as like bellow;

Matrix A is,

3  4  5

6  7  8

2  3  4

And matrix B is,

1  2  3

4  5  6

7  8  9

So, the sum of two matrix A + B will be,

4    6    8

10  12  14

9   11   13

We can write the code in C for this purpose of add two matrix in different way. You can also use function or recursion for this purpose. Let’s see a program bellow which will determine the sum to two matrix A and B.

We recommend you to learn our two dimensional array chapter to get better understanding here.

Note : The row and column of first matrix and the row and column of second matrix must have to be equal to add them. Otherwise we can not able to add them here.

C program to add two matrix

// c add two matrix program

#include <stdio.h>

int main(){
   int first_arr[20][20], second_arr[20][20], result_arr[20][20], row, col, x, y;

   printf("Enter how many row and column of each matrix : ");
   scanf("%d %d", &row, &col);

   printf("\nEnter the elements for first matrix here : \n");

   for(x = 0; x < row; x++){

      for(y = 0; y < col; y++){
         scanf("%d", &first_arr[x][y]);
      }
   }
   printf("\nEnter the elements for second matrix here : \n");

   for(x = 0; x < row; x++){

      for(y = 0; y < col; y++){
         scanf("%d", &second_arr[x][y]);
      }
   }

   printf("\nSum of two matrix is as follows : \n");

   for(x = 0; x < row; x++) {

      for(y = 0 ; y < col; y++) {
         result_arr[x][y] = first_arr[x][y] + second_arr[x][y];
         printf("%d\t", result_arr[x][y]);
      }

      printf("\n");
   }
   return 0;
}

Output of this add matrix program:

c-program-to-add-two-matrix-add-matrix-program-in-c

Similarly you can subtract a matrix from other. Everything will be same for subtract a matrix from another. See the program for subtracting a matrix from other.

C program to subtract two matrix

// c program to subtract two matrix

#include <stdio.h>

int main(){
   int first_arr[20][20], second_arr[20][20], result_arr[20][20], row, col, x, y;

   printf("Enter how many row and column of each matrix : ");
   scanf("%d %d", &row, &col);

   printf("\nEnter the elements for first matrix here : \n");

   for(x = 0; x < row; x++){

      for(y = 0; y < col; y++){
         scanf("%d", &first_arr[x][y]);
      }
   }
   printf("\nEnter the elements for second matrix here : \n");

   for(x = 0; x < row; x++){

      for(y = 0; y < col; y++){
         scanf("%d", &second_arr[x][y]);
      }
   }

   printf("\nResult matrix after subtracting is : \n");

   for(x = 0; x < row; x++) {

      for(y = 0 ; y < col; y++) {
         result_arr[x][y] = first_arr[x][y] - second_arr[x][y];
         printf("%d\t", result_arr[x][y]);
      }

      printf("\n");
   }
   return 0;
}

Output of subtract matrix program:

Enter how many row and column of each matrix : 3 3

Enter the elements for first matrix here :
9 8 7
8 7 6
7 6 5

Enter the elements for second matrix here :
1 2 3
2 3 4
3 4 5

Result matrix after subtracting is :
8       6       4
6       4       2
4       2       0

Previous pageDelete element from array

Next pageTranspose matrix