My Subject Blog

C Program for Matrix Multiplication

/*
Write a program to Multiply two 3X3 Matrices. (Matrix Multiplication)
*/

#include<stdio.h>
#define size 3
void main()
{
int c[size][size],a[size][size],b[size][size],i,j,k,m1,n1,m2,n2;
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++)
    {
      c[i][j]=0;
      for(k=0;k<n1;k++)
      {
        c[i][j] += a[i][k]*b[k][j];
      }
   }
 }

for(i=0;i<m1;i++)
  {
  for(j=0;j<n2;j++)
   {
   printf("%d\t",c[i][j]);
   }
   printf("\n");
  }
}

return 0;
}

Comments

Most Visited Post

C Program to Find Largest Odd Number from Array

Followers