c program for matrix multiplication



c program for matrix multiplication. A matrix is an arrangement of items into set of rows and columns within a table. Matrix can be represented is a 2-Dimensional array. We can perform various operation such as addition, subtration, multiplication, transpose etc. You can find other matrix operation c program here. Matrix multiplication can be done between two matrix if and only if “the number of columns of first matrix should be equal to the number of rows in second matrix” i.e

For example :
if matrix A[m][n] has, m rows and n columns , the matrix B[g][l] should have its row value g=n.
and this matrix multiplication will generate a new matrix C[m][l].
where m= number of rows in first matrix
and   l= number of columns in second matrix

c program for matrix multiplication

#include<stdio.h>
#include<conio.h>
int main()
{
     int a[10][10], b[10][10], c[10][10],m,n, i, j, k, l,g;
     printf("Enter the number of rows and column of first matrix\n");
     scanf("%d%d",&m,&n);
     printf("\nEnter the elements of first %dx%d matrix\n",m,n);
     for (i=0; i< m; i++)
     {
         for(j=0; j<n; j++)
         {
                  scanf("%d",&a[i][j]);
         }
     }
     s1 : printf("\nEnter the number of rows and column of second matrix\n");
     scanf("%d%d",&g,&l);
     if(n!=g)
     {
             printf("\nIn matrix multiplication first column and second row number should be the same \nRenter ");
             getch();
             goto s1;
     }
     printf("\nEnter the elements of second %dx%d matrix",n,l);
     for(i = 0; i <n; i++)
     {
           for (j = 0; j < l; j++)
           {
           scanf("%d", &b[i][j]);
           }
     }
     printf("\nThe first matrix is :-\n");
     for (i = 0; i < m; i++)
     {
            for (j = 0; j < n; j++)
            {
            printf("\t%d", a[i][j]);
            }
     printf("\n");
     }
     printf("\nThe second matrix is :-\n");
     for (i = 0; i < n; i++)
     {
            for (j = 0; j < l; j++)
            {
            printf("\t%d", b[i][j]);
            }
     printf("\n");
     }

    printf("\nMultiplication of the two matrices is as follows:\n");
    for (i = 0;i < m; i++)
    { 
         printf("\n");
         for (j = 0; j < l; j++)
         {
              c[i][j]=0;
              for(k=0;k<n;k++)
                   c[i][j] = c[i][j]+a[i][k] * b[k][j];
              printf("\t%d", c[i][j]);
         }
    }
    getch();
}

Output of c program for matrix multiplication:

c program for matrix multiplication
c program for matrix multiplication

 




"Please Do Like Facebook Page and follow us on Twitter so that you can actively participate and develop skills in programming. If you find above post interesting do share the webpage."
This entry was posted in c program and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>