# C Program to Add Two Matrices

Hello readers, today we will learn how to write a program to add two matrices using C Programming language.

Two or more matrices of the same order can be added by adding the corresponding elements of the matrix. If A = [aij] and B = [bij] are the two matrices, having the same number of rows and columns, then the addition the matrices A and B is:

A + B = [aij] + [bij] = [aij + bij]

For example: Suppose we have two matrices A and B such that:

```A = 1  2  3 ,B = 5  8  4, then A + B = (1+2) (2+8) (3+4)  =  3 10 12
4  5  7      9  6  2               (4+9) (5+6) (7+2)     13 11 9
```

This program asks the user to enter the order of two matrices, and elements of each of the matrices. And, then it adds both of them using the above formula.

So, without any delay, let’s begin this tutorial.

## C Program to Add Two Matrices

C Program

```// C Program to Add Two Matrices
#include <stdio.h>
int main(){
int row, col, i, j, a, b, sum;

printf("Enter the no. of rows (between 1 and 25): ");
scanf("%d", &row);
printf("Enter the no. of columns (between 1 and 25): ");
scanf("%d", &col);

printf("Enter elements of the first matrix: \n");
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of the second matrix: \n");
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
scanf("%d", &b[i][j]);
}
}

for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
sum[i][j] = a[i][j] + b[i][j];
}
}

// Displaying the result
printf("Sum of two matrices: \n");
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
```

Output

``````Enter the no. of rows (between 1 and 25): 2
Enter the no. of columns (between 1 and 25): 3

Enter elements of the first matrix:
2 4 6
8 10 12

Enter elements of the second matrix:
1 3 5
7 9 11

Sum of two matrices:
3 7 11
15 19 23
``````

Explanation

```    int row, col, i, j, a, b, sum;
```

In this program, we have declared four int data type variables named row, col, i, j and three arrays.

```    // Asking for input
printf("Enter the no. of rows (between 1 and 25): ");
scanf("%d", &row);
printf("Enter the no. of columns (between 1 and 25): ");
scanf("%d", &col);
```

Then, the user is asked to enter the no. of rows and columns of the matrix.

```    printf("Enter elements of the first matrix: \n");
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of the second matrix: \n");
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
scanf("%d", &b[i][j]);
}
}
```

Now, the user is asked to enter the elements of each of the matrices.

```    // Adding two matrices
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
sum[i][j] = a[i][j] + b[i][j];
}
}
```

We add these two matrices by using the formula:

A + B = [aij] + [bij] = [aij + bij]

```    // Displaying the result
printf("Sum of two matrices: \n");
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
printf("%d ", sum[i][j]);
}
printf("\n");
}
```

Finally, the sum of two matrices is displayed on the screen in tabular form using printf() function.

## Conclusion

I hope after reading this post, you understand how to add two numbers using C Programming language.

If you have any doubt regarding the program, let us know in the comment section. We will be delighted to solve your query.