In this post, we will see the C program to find the matrix addition. Matrix Addition in C | Addition of two matrix in C | C program to add two matrices | Add two matrix in C.

Let A =[aij] and B =[bij] be m × n matrices. The sum of A and B, denoted by A + B, is the m × n matrix that has aij + bij as its (i, j)th element. In other words, A + B =[aij + bij].

The sum of two matrices of the same size is obtained by adding elements in the corresponding positions.

Condition for matrix addition:- Both matrices should have the same size.

Matrices of different sizes cannot be added, because the sum of two matrices is defined only when both matrices have the same number of rows and the same number of columns.

```     a11  a12
A =
a21  a22```
```      b11  b12
B =
b21  b22```

The addition of A and B can be calculated as,

```          a11+b11  a12+b12
A + B =
a21+b21  a22+b22```

Simple Program

This is the simple C Program without using any function. The values for the matrices are already given in the program itself.

``````#include <stdio.h>
int main()
{
// matrix A and B
int a = {{1,2},{3,4}};
int b = {{5,6},{7,8}};

// declare matix C
int c;

// add both matrix A and B
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
// add & store to matrix C
c[i][j] = a[i][j] + b[i][j];
}
}

// display matrix
printf("Resultant Matrix: \n");
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n"); // new line
}

return 0;
}``````

Output:-

Resultant Matrix:
6 8
10 12

In this program, we had taken two 2×2 matrix. Since both matrices are of the same size therefore matrix addition is possible, and a new resultant matrix also will be the same size.

## Algorithm for Matrix Addition in C Program

a) Take three matrices (matrix1 & metrix2 & matrix3)
b) Use the nested loop to iterate the matrix1 and matrix2
c) Find the sum of crosspounding elements in matrix1 & matrix2 and assign them to matrix3

### Function

``````// C Function to Add Two Matrix
int c, int row, int column)
{
// outer loop for row
for(int i=0; i< row; ++i)
{
// inner loop for column
for(int j=0; j< column; ++j)
{
// add & store values to matrix C
c[i][j] = a[i][j] + b[i][j];
}
}
}``````

The addMatrix() function takes 5 parameters, the first three parameters for matrix a, b, c, and the remaining two parameters for row and column. Its return type is void, and it returns nothing. The arrays are implicit pointers and therefore internal values are stored to the original variables.

Time complexity:- O(N2)

If both matrix has same row and column size then move to the next step else matrix addition is not possible, return back.

## Addition of Two Matrix in C using Function

In this C program, We will use the user-defined functions in these programs, so the function in C knowledge should be there. Along with this, you must know how to pass the multidimensional array to a function.

C Program for Matrix Addition using Functions

``````#include <stdio.h>
// function to add two matrix
int c, int row, int column)
{
for(int i=0; i< row; ++i)
{
for(int j=0; j< column; ++j)
{
// add & store to matrix C
c[i][j] = a[i][j] + b[i][j];
}
}
}
void readMatrix(int matrix, int row, int column)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
scanf("%d", &matrix[i][j]);
}
}
}
// function to display matrix
void displayMatrix(int matrix, int row, int column)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
printf("%d ", matrix[i][j]);
}
printf("\n"); // new line
}
}
// main function
int main()
{
// declare matrix matrix A, B, & C
int a; // first matrix
int b; // second matrix
int c; // resultant matrix

// read the size of matrices
int row, column;
printf("Enter Row and Column Sizes: ");
scanf("%d %d", &row, &column);

// read matrix A and B
printf("Enter Matrix-1 Elements: \n");
printf("Enter Matrix-2 Elements: \n");

// add both matrix A and B

// display resultant matrix
printf("Resultant Matrix: \n");
displayMatrix(c, row, column);

return 0;
}``````

Output for different input values,

Enter Row and Column Sizes: 2 2
Enter Matrix-1 Elements:
1 3
7 5
Enter Matrix-2 Elements:
6 8
4 2
Resultant Matrix:
7 11
11 7

Enter Row and Column Sizes: 3 3
Enter Matrix-1 Elements:
1 2 3
4 5 6
7 8 9
Enter Matrix-2 Elements:
4 6 8
7 5 3
9 5 1
Resultant Matrix:
5 8 11
11 10 9
16 13 10

If we are asking row and column size seperatly then before finding the addition of two matrix, first we should check both are having same size or not i.e. the row and column of both matrix must be equal else matrix addition is not possible. Find the row and column size of both matrix, if they are same then declare a new matrix with the same row and column size, which will store the resultant values. Then proced further to find the addition of matrices.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!