C Program to Add Two Matrix
Question: Write a program in C to add two matrix having size M by N. Where M and N are given by user.
#include<stdio.h>
int main()
{
int i,j,m,n;
float a[10][10], b[10][10], sum[10][10];
printf("Enter row and column size:\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix:\n");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f", &a[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
printf("b[%d][%d]=",i,j);
scanf("%f", &b[i][j]);
}
}
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
sum[i][j] = a[i][j]+b[i][j];
}
}
printf("Added matrix is:\n");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
printf("%f\t", sum[i][j]);
}
printf("\n");
}
return 0;
}