Site icon DataFlair

C Program to Multiply Two Matrix

Program 1

// Program for matrix multiplication
#include<stdio.h>
#include<conio.h>
int main()
{
      system("cls");
     int a[50][50],b[50][50],c1[50][50],m,n,r,c,k;

     xyz:printf("Enter value of row and column");
     scanf("%d%d",&m,&n);
     if(m>50 || n>50)
     {
         printf("Invalid limit of row or column\n");
         goto xyz;
     }
     printf("\nEnter elements in first matrix\n");
     for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
        {
            scanf("%d",&a[r][c]);
        }
     }
      printf("\nEnter elements in second matrix\n");
     for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
        {
            scanf("%d",&b[r][c]);
        }
     }
     printf("\nElements of first matrix\n");
     for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
        {
            printf("%4d",a[r][c]);
        }
        printf("\n");
     }

    printf("\nElements of second matrix\n");
     for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
        {
            printf("%4d",b[r][c]);
        }
        printf("\n");
     }
     // Matrix multiplication
   for(r=0;r<m;r++)
   {
      for(c=0;c<n;c++)
      {
         for(k=0;k<m;k++)
         {
            c1[r][c]=c1[r][c]+(a[r][k]*b[k][c]);
         }
      }
   }
   printf("\nResult is:  \n");
     for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
        {
            printf("%4d",c1[r][c]);
        }
        printf("\n");
     }
}

 

Exit mobile version