C Program to add two matrix
Program
#include<stdio.h>
void main()
{
int i, j;
int mat1[10][10], mat2[10][10], mat3[10][10];
int row, col;
printf("Enter the number of rows for both matrix:\t");
scanf("%d", &row);
printf("Enter the number of columns for both matrix:\t");
scanf("%d", &col);
printf("Enter %d elements for matrix 1\n", row * col);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter %d elements for matrix 2\n", row * col);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
scanf("%d", &mat2[i][j]);
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
printf("The Addition of two Matrices is:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%d\t", mat3[i][j]);
}
printf("\n");
}
printf("\n");
}
Output
Enter the number of rows for both matrix: 2
Enter the number of columns for both matrix: 2
Enter 4 elements for matrix 1
12
56
77
32
Enter 4 elements for matrix 2
88
45
72
21
The Addition of two Matrices is:
100 101
149 53