C Program Matrix Multiplication 2D array

This program is for matrix multiplication using 2 D matrix. This is a part of Mumbai University MCA Colleges C programs MCA Sem 1
#include<stdio.h>
#include<conio.h>
#define size 5
void main()
{
int c[size][size],a[size][size],b[size][size],i,j,sum=0,k,m1,n1,m2,n2;
clrscr();
printf("Enter the order of 1st matrix \n");
printf("Rows \t");
scanf("%d",&m1);
printf("\n Column\t");
scanf("%d",&n1);
printf("Enter the order of 2st matrix \n");
printf("Rows\t");
scanf("%d",&m2);
printf("\n Column\t");
scanf("%d",&n2);
if(n1!=m2)
{
printf("\nMatrix multiplication is invalid");
}
else
{
printf("\nEnter the elements of 1st array");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nEnter the elements of 2nd array");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

printf("Multiplication is \n");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
for(k=0;k<n1;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
sum=0;
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
getch();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a comment