C Program code For Multiplication of Matrices

Question Types:

Program Code for multiplication of matrices of any order using c programming language ?
Print multiplication of matrices using c language and also show if multiplication is not possible ?

Program Code:

#include<stdio.h>
int main()
{
int a[][] , b[][] , c[][];
int i, j , k ;
int n, m, p , q;
printf("Enter no.of rows and coloumns of Matrix A :\n ");
scanf("%d %d",&n,&m);
printf("\nEnter no.of rows and coloumns of Matrix B :\n ");
scanf("%d %d",&p,&q);
if(m==p)
{
// Matrix a ==> Getting elements from user
    printf("\n\nEnter elements for matrix A :\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++){
scanf("\t%d",&a[i][j]);
}
}

// Matrix b ==> Getting elements from user
    printf("\nEnter elements for matrix B :\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++){
scanf("\t%d",&b[i][j]);
}
}
// Matrix a ==> displaying code
    printf("\n\nMatrix A: \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++){
printf("\t %d\t",a[i][j]);
}
printf("\n");
}

// Matrix b ==> displaying code
    printf("\n\nMatrix B: \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++){
printf("\t %d\t",b[i][j]);
}
printf("\n");
}
// Matix c = Matrix a * Matrix b
   for(i=0;i<m;i++)
   {
       for(j=0;j<p;j++)
      {
             c[i][j]=0;
      for(k = 0; k<m;k++)
           {
             c[i][j] =c[i][j] +( a[i][k] * b[k][j] );
   
           }     
       
    }
   
   }
   
// Matrix C ==> Displaying code
    printf("\n\nMatrix C = Matirx A * Matrix B :\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++){
printf("\t %d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Multiplication is not possible ,,,,,");
return 0;
}

Output:

.exe file 

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.