C Program to Multiply Two PxQ & QxR Matrix


Question: Write a program in C to multiply two p x q and q x r matrix.


#include<stdio.h>


int main()
{
 int i,j,k,r1,c1,r2,c2;
 float a[3][3], b[3][3], mul[3][3];
 
 printf("Enter row and column of first matrix\n");
 scanf("%d%d", &r1, &c1);
 printf("Enter row and column of second matrix\n");
 scanf("%d%d", &r2, &c2);
 
 if(c1==r2)
 {
  printf("Enter elements of first matrix:\n");
  for(i=0;i< r1;i++)
  {
   for(j=0;j< c1;j++)
   {
    printf("a[%d][%d]=",i,j);
    scanf("%f", &a[i][j]);
   }
  }
  
  printf("Enter elements of second matrix:\n");
  for(i=0;i< r2;i++)
  {
   for(j=0;j< c2;j++)
   {
    printf("b[%d][%d]=",i,j);
    scanf("%f", &b[i][j]);
   }
  }
  for(i=0;i< r1;i++)
  {
   for(j=0;j< c2;j++)
   {
    mul[i][j] = 0;
    for(k=0;k< r2;k++)
    {
     mul[i][j] = mul[i][j] + a[i][k]*b[k][j];
    }
   }
  }
  
  printf("Multiplied matrix is:\n");
  for(i=0;i< r1;i++)
  {
   for(j=0;j< c2;j++)
   {
    printf("%f\t", mul[i][j]);
   }
   printf("\n");
  }
 }
 else
 {
  printf("Dimension do not match for multiplication.");
 }
 return 0;
}