Addition of Two Matrices

Matrix Addition in C

Matrix addition in C language to add two matrices, i.e., compute their sum and print it. A user will input the order of matrix (number of rows and columns) and two matrices. For example, if the order is 2, 2, i.e., two rows and two columns and the matrices are:

First matrix:
1 2
3 4
Second matrix:
4 5
-1 5
then the output will be:
5 7
2 9

#include<stdio.h>
#include<conio.h>

void main()
{
int m1[5][5],m2[5][5],sum[5][5];
int i,j,r1,c1,r2,c2;
printf(" enter the rows and column of matrix 1\n")';
scanf("%d%d",&r1,&c1);

printf("enter the elements of  matrix1\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}

printf("enter the rows and column of matrix 2\n");
scanf("%d%d",&r2,&c2)
printf("enter the elements of matrix 2\n");

for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",m2[i][j]);
}
}

if(r1==r2&&c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
sum[i][j]=m1[i][j]+m2[i][j];
}
}
}
else
{
printf(" Sum not found");
}
printf("addition of two matrices\n");

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d",&sum[i][j]);
}
printf("\n");
}
getch();
}

Output:-



No comments:

Post a Comment