Matrix Addition and Multiplication info
Matrix
Addition & Multiplication
In
this article we will see the Addition and Multiplication of the two matrices
//this
program is designed to demonstrate Matrix Addition and Multiplication
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a[3][3],b[3][3],add[3][3],mult[3][3];
clrscr();
//Accept Matrix A
printf("\n Enter First Matrix
: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
//Accept Matrix B
printf("\n Enter Second
Matrix : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
//Addition of Matrices
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
add[i][j]=a[i][j]+b[i][j];
}
}
//Display Addition
printf("\n Display Matrix
Elements ");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
{
printf("\t
%d",add[i][j]);
}
}
//Matrix Multiplication
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
sum=sum+a[i][k]*b[k][j];
}
mult[i][j]=sum;
sum=0;
}
}
//display Multiplication
printf("\n Multiplication is
");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
{
printf("\t
%d",mult[i][j]);
}
}
getch();
}
|
Output: We are
accepting two matrices from the user and first performing Addition and then
Multiplication, the output will be as follow,
Comments
Post a Comment