Discussion Forums

New User? Please read instructions!

C program for matrix multiplication  

  RSS

thiyagarajan.s
Active Member
Joined: 1 month  ago
Posts: 14
27/01/2017 10:06 am  

C program for matrix multiplication

Edited: 4 weeks  ago

ReplyQuote
prakash
Eminent Member
Joined: 1 month  ago
Posts: 20
17/02/2017 7:46 am  

C program for matrix multiplication:

 

#include <stdio.h> 

int main()

{

    int m, n, p, q, c, d, k, sum = 0;

    int m1[10][10], m2[10][10], m3[10][10];

  

    printf("Please enter the number of rows of 1st matrix\n");

    scanf("%d", &m);

    printf("Please enter number of columns of 1st matrix\n");

    scanf("%d", &n);

    printf("Please enter the elements of 1st matrix one by one\n");

 

    for (  c = 0 ; c < m ; c++ )

    {

         for ( d = 0 ; d < n ; d++ )

         {

               scanf("%d", &m1[c][d]);

         }

    }    

 

     printf("Please enter the number of rows of 2nd matrix\n");

     scanf("%d", &p);

     printf("Please enter number of columns of 2nd matrix\n");

     scanf("%d", &q);

 

     if ( n != p )

    {

          printf("The entered matrices can't be multiplied each other.\n");

          printf("To multiply two matrices of X and Y, the number of columns in X must be equal to the number of rows in Y");

    }

  

    else

    {

         printf("Please enter the elements of 2nd matrix one by one\n");

         for ( c = 0 ; c < p ; c++ )

         for ( d = 0 ; d < q ; d++ )

         scanf("%d", &m2[c][d]);

 

         for ( c = 0 ; c < m ; c++ )

         {

              for ( d = 0 ; d < q ; d++ )

              {

                        for ( k = 0 ; k < p ; k++ )

                         {

                                   sum = sum + m1[c][k]*m2[k][d];

                         }

                         m3[c][d] = sum;

                         sum = 0;

               }

        }

 

        printf("Multiplication of entered matrices:\n");

 

        for ( c = 0 ; c < m ; c++ )

        {

              for ( d = 0 ; d < q ; d++ )

                      printf("%d\t", m3[c][d]);

              printf("\n");

        }

    }

 

    return 0;

}

 

Output:

Please enter the number of rows of 1st matrix

3

Please enter number of columns of 1st matrix

3

Please enter the elements of 1st matrix one by one

1

2

3

1

2

3

1

2

3

Please enter the number of rows of 2nd matrix

3

Please enter number of columns of 2nd matrix

3

Please enter the elements of 2nd matrix one by one

4

5

6

4

5

6

4

5

6

Multiplication of entered matrices:

24  30  36

24  30  36

24  30  36

 

Edited: 1 week  ago

ReplyQuote
  
Working

Please Login or Register

Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!