top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to multiply two matrix using C, any sample program would be help?

+3 votes
333 views
How to multiply two matrix using C, any sample program would be help?
posted Jan 17, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

/* C program to multiply the two matrices */

#include <stdio.h>
#include<conio.h> 
void main()
{
   int m, n, p, q, c, d, k, sum = 0;
   int ar1[10][10], ar2[10][10], mul[10][10];

   printf("Enter the number of rows and columns of first matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for (  c = 0 ; c < m ; c++ )
   {
       for ( d = 0 ; d < n ; d++ )
       {
           scanf("%d", &ar1[c][d]);
       }
   }

   printf("Enter the number of rows and columns of second matrix\n");
   scanf("%d%d", &p, &q);

   if ( n != p )
   {
      printf("Multiplication is not possible of the two matrices \n");
   }
   else
   {
      printf("Enter the elements of second matrix\n");

      for ( c = 0 ; c < p ; c++ )
      {
         for ( d = 0 ; d < q ; d++ )
         {
            scanf("%d", &ar2[c][d]);
         }
      }

      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < q ; d++ )
         {
            for ( k = 0 ; k < p ; k++ )
            {
               sum = sum + ar1[c][k]*ar2[k][d];
            }

            mul[c][d] = sum;
            sum = 0;
         }
      }

      printf("Product of entered matrices:-\n");

      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < q ; d++ )
            printf("%d\t", mul[c][d]);

         printf("\n");
      }
   }
   getch();
}

Output –

Enter the number of rows and columns of first matrix – 2 2
Enter the elements of first matrix –

2 3
4 3

Enter the number of rows and columns of second matrix – 2 3

5 7 2
5 6 4

Product of entered matrices is –

25 32 16
35 46 20
answer Jan 20, 2016 by Mohammed Hussain
Similar Questions
+3 votes
0 votes

You are given 2 long integers having n digits each and you are required to multiply them using C.

Assumptions
Numbers are represented in an array of size n .
Calculate the time complexity using traditional divide and conquer

...