Matrix is an important chapter in the Mathematics that we study in 9th Grade. Adding Two Matrices is very simple, it is the same as simple Addition. In this C Programming example, we will write a C program to add two Matrices.
To begin with, we must have the knowledge of the following concepts:
- Array
- Two-Dimensional Array
Algorithm to Add Two Matrices
Here’s the algorithm to add two Matrices:
- START
- Input the matrix 1 elements.
- Input the matrix 2 elements.
- Repeat from i = 0 to 3
- Repeat from j = 0 to 3
- mat3[i][j] = mat1[i][j] + mat2[i][j]
- Print mat3.
- STOP
C Program to Add Two Matrices
Here’s the C Program to add two Matrices:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, mat1[3][3], mat2[3][3];
printf("Enter first matrix: \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("\nEnter second matrix: \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &mat2[i][j]);
}
}
printf("\nmat1 + mat2 = \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d ", mat1[i][j] + mat2[i][j]);
}
printf("\n");
}
getch();
}
Output
Here’s the output of the above program:
