Code Scrappers


Matrix Multiplication - C Program


                    #include<stdio.h>
                    #include<stdlib.h>

                    void main()
                    {
                        int m1[8][8], m2[8][8], m3[8][8];

                        int i, j, k, m, n, p, q, sum;

                        printf("\nEnter the no. rows of first matrix : ");

                        scanf("%d",&m);

                        printf("\nEnter the no. columns of first matrix : ");

                        scanf("%d",&n);

                        printf("\nEnter the no. rows of second matrix : ");

                        scanf("%d",&p);

                        printf("\nEnter the no. columns of second matrix : ");

                        scanf("%d",&q);

                        sum = 0;

                        if(n != p)
                        {
                            printf("\n matrix mutiplication not possible");

                            exit(0);

                        }

                        else
                        {
                            printf("\n enter the elements of first matrix:");

                            for(i=0;i<m;i++)
                            {
                                for(j=0;j<n;j++)
                                {
                                    scanf("%d",&m1[i][j]);

                                }

                            }

                            printf("\n enter the elements of Second matrix:");

                            for(i=0;i<p;i++)
                            {
                                for(j=0;j<q;j++)
                                {
                                    scanf("%d",&m2[i][j]);

                                }

                            }

                            for(i=0;i<m;i++)
                            {
                                for(j=0;j<q;j++)
                                {
                                    m3[i][j]=0;

                                    for(k=0;k<n;k++)
                                    {
                                        m3[i][j]= m3[i][j]+m1[i][k]*m2[k][j];

                                    }

                                }

                            }

                        }

                        printf("\nmultiplication of two matrix is\n");

                        for(i=0;i<m;i++)
                        {
                            printf("\n");

                            for(j=0;j<q;j++)
                            {
                                printf("%d\t",m3[i][j]);

                            }

                        }

                    }






                



Output

    Enter the no. rows of first matrix : 2

    Enter the no. columns of first matrix : 2

    Enter the no. rows of second matrix : 2

    Enter the no. columns of second matrix : 2

    enter the elements of first matrix:1
    2
    3
    4

    enter the elements of Second matrix:1
    2
    3
    4

    multiplication of two matrix is

    7       10
    15      22