Wednesday, February 19, 2014

C Program to Perform Matrix Multiplication

with 0 Comment
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50][50],b[50][50],x[50][50];
int i,j,r,r1,c,c1,k;
clrscr();
printf("\t Matrix Multiplication:\n");
printf("\t ***********\n");
printf("Input:\n");
printf("****\n");
printf("\nEnter the order of first matrix:\n");
scanf("%d %d",&r,&c);
printf("\nEnter the order of second matrix:\n");
scanf("%d %d",&r1,&c1);
if(c==r1)
{
printf("\nEnter the element for the first matrix:\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nFirst Matrix is:\n");
printf("\n*********\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nEnter the element for the first matrix:\n");
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n Second Matrix is:\n");
printf("\n***********\n");
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\nOutput:\n");
printf("\n*****\n");
printf("\nMultiplication of Two Matrix:\n");
printf("\n***************\n");

for(i=1;i<=r;i++)
{
for(j=1;j<=c1;j++)
{
x[i][j]=0;
for(k=1;k<=c;k++)
{
x[i][j]=x[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=1;i<=r;i++)
{
for(j=1;j<=c1;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
}
else
{
printf("\nOutput:\n");
printf("\n*****\n");
printf("Matrix Multiplication is Not possible:");
}
getch();
}

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive