Matrix Multiplication in C

In this C program, We will show “How to build logic for Matrix Multiplication “. We, also see, “How to print the the new multiplied Matrix by taking user input”. So, let’s do it …

Matrix Multiplication in C by Taking User Input

//C program for Matrix Multiplication.
#include<stdio.h>
int main()
{
    int i, j, k, r1, r2, c1, c2;
    int first[100][100], second[100][100], result[100][100], sum=0;

    printf("Enter the number of rows and column of first Matrix: ");
    scanf("%d %d",&r1,&c1);

    printf("Enter the number of rows and column of second Matrix: ");
    scanf("%d %d",&r2,&c2);

    while(c1!=r2)
    {
        printf("The column number of first Matrix and the row number of second Matrix are not same.   \n");

        printf("Enter the number of rows and column of first Matrix: ");
        scanf("%d %d",&r1,&c1);

        printf("Enter the number of rows and column of second Matrix: ");
        scanf("%d %d",&r2,&c2);
    }


//Scanning the value of first Matrix.
    for(i=0; i<r1; i++)
    {
      for(j=0; j<c1; j++)
      {
        printf("Enter The Elements of first[%d][%d] = ", i, j);
        scanf("%d", &first[i][j]);
      }
    }
    printf("\n");

//scanning the value of second Matrix.
    for(i=0; i<r2; i++)
    {
        for(j=0; j<c2; j++)
        {
            printf("Enter the elements of second[%d][%d] = ", i, j);
            scanf("%d",&second[i][j]);
        }
    }
    printf("\n");


//printing the elements of first matrix.
    printf("Elements of first Matrix: \n");
    for(i=0; i<r1; i++)
    {
        for(j=0; j<c1; j++)
        {
            printf("%d ",first[i][j]);
        }
        printf("\n");

    }


//printing the elements of first matrix.
    printf("\nElements of second Matrix: \n");

    for(i=0; i<r2; i++)
    {
        for(j=0; j<c2; j++)
        {
            printf("%d ",second[i][j]);
        }
        printf("\n");
    }


//Matrix multiplication.
    for(i=0; i<r1; i++)
    {
        for(j=0; j<c2; j++)
        {
            for(k=0; k<c1; k++)
            {
                sum = sum + first[i][k] * second[k][j];
            }
            result[i][j] = sum;
            sum = 0;
        }
    }

//printing the miltiplication result.
    printf("\nResult Matrix: \n");

    for(i=0; i<r1; i++)
    {
        for(j=0; j<c2; j++)
        {
            printf("%d ",result[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Or,

#include<stdio.h>
int main()
{
    int a[100][100],b[100][100],c[100][100];
    int i,j,k,sum,r1,c1,r2,c2;
    printf("Enter row and column for matrix A : ");
    scanf("%d %d",&r1,&c1);
    for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                {
                    printf("Enter element [%d][%d] = ",i,j);
                    scanf("%d",&a[i][j]);
                }
        }
    printf("\n\nMatrix A : \n\n");

       for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                {
                    printf("%5d",a[i][j]);
                }
            printf("\n");
        }
     printf("\n\nEnter row and column for matrix B : ");
    scanf("%d %d",&r2,&c2);
    for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                {
                    printf("Enter element [%d][%d] = ",i,j);
                    scanf("%d",&b[i][j]);
                }
        }
    printf("\n\nMatrix B : \n\n");

       for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                {
                    printf("%5d",b[i][j]);
                }
            printf("\n");
        }
    if(c1==r2)
    {

      for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
                {
                    sum=0;
                    for(k=0;k<c1;k++)
                        sum=sum+a[i][k]*b[k][j];
                    c[i][j]=sum;
                }
        }
        printf("\n\nMatrix C : \n\n");

       for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
                {
                    printf("%5d",c[i][j]);
                }
            printf("\n");
        }
    }
    else
        printf("Can not Multiply A & B :-( ");
    return 0;
}

Keep Learning with PrologiCode.

Happy Coding.

Leave a Comment