Program to Add Two Matrics using C
PROGRAM TO ADD TWO MATRICS USING C:
AIM:- How to add two matrics using C language.
Mathematical Working::
We can only add or subtract matrices if their dimensions are the same. To add matrices, we simply add the corresponding matrix elements together. To subtract matrices, we simply subtract the corresponding matrix elements together. Not only can we add and subtract matrices, but we can solve matrix equations as well.
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j;
clrscr();
printf("\nenter the elements in the 1st matrix:");
scanf("\%d%d",&r1,&c1);
printf("\n enter the elements in the 2nd matrix:");
scanf("%d%d" ,&r2,&c2);
if((r1==r2)&&(c1==c2))
{
printf("enter elements in 1st matrix:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("\n%d",&a[i][j]);
}
printf("\nenter elements in 2nd matrix:");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("\n sum of two matrix's:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d",c[i][j]);
printf("\t\n");
}
}
else
printf("\n wrong order entered!");
getch();
}
OUTPUT:
Comments
Post a Comment