C Program for matrix multiplication

      Multiplication of two matrices is possible only if the number of columns in the first matrix is equal to the number of rows in the second matrix otherwise multiplication is not possible. If the first matrix is the order of row1xcol1 and second matrix is the order of row2xcol2 then matrix multiplication is possible only if col1==row2 and the order of resulting matrix will be row1xcol2. This program initially checks whether matrix multiplication is possible or not. If it is possible then mulitiplication is computed with the help of another inner for loop.

matrix multiplication program in c



Source Code :

#include<stdio.h>
#include<tstring.h>
#include<stdlib.h>
#define row 10
#define col 10
int main()
{
    int row1, col1;
    int row2, col2;
    int i,j,k;
    float mat1[row][col];
    float mat2[row][col];
    float mat_res[row][col];
    printf("\n Input the row of the matrix->1:");
    scanf("%d", &row1);
    printf("\n Input the col of the matrix->1:");
    scanf("%d", &col1);
    printf("\n Input data for matrix-> 1\n");
    for(i = 0 ; i< row1; i++)
    {
        for(j = 0 ; j < col1; j++)
        {
            printf("Input Value for: %d: %d:", i+1, j+1); //Step1
            scanf("%f", &mat1[i][j]);
        }
    }
    printf("\n Input the row of the matrix->2:");
    scanf("%d", &row2);
    printf("\n Input the col of the matrix ->2:");
    scanf("%d", &col2);
    printf("\n Input data for matrix-> 2\n");
    for(i = 0 ; i < row2; i++)
    {
        for(j = 0 ; j < col2; j++)
        {
            printf("Input Value for: %d: %d:", i+1, j+1);
            scanf("%f", &mat2[i][j]);                     //Step2
        }
    }
    printf("\n Entered Matrix First is:\n");
    for(i = 0; i < row1; i++)
    {
        for(j = 0; j < col1; j++)
            printf("%f", mat1[i][j]);
        printf("\n");
    }
    printf("\n Entered Matrix Two is: \n");
    for(i = 0; i < row2; i++)
    {
        for(j = 0; j < col2; j++)
            printf("%f", mat2[i][j]);
        printf("\n");
    }
    if(col1 == row2)                                       // Step3
    {
        printf("\n Multiplication is possible and the Result is as follows\n");
        for(i=0; i < row1; i++)
            for(j=0; j < col2; j++)
            {
                mat_res[i][j] = 0;
                for(k = 0; k < col1; k++)
                    mat_res[i][j] += mat1[i][k] * mat2[k][j];          // Step4
            }
        for(i = 0; i < row1; i++)
        {
            for(j = 0; j < col2; j++)
                printf("%f", mat_res[i][j]);                 // Step5
            printf("\n");
        }
    }
    else
        printf("\n Multiplication is not possible");
    return 0;
}


edit-code-image

Sample Test cases:

1. 
 Input the row of the matrix->1:3
 Input the col of the matrix->1:3
 Input data for matrix-> 1
Input Value for: 1: 1: 2
Input Value for: 1: 2: 3
Input Value for: 1: 3: 4
Input Value for: 2: 1: 5
Input Value for: 2: 2: 6
Input Value for: 2: 3: 7
Input Value for: 3: 1: 1
Input Value for: 3: 2: 2
Input Value for: 3: 3: 4

 Input the row of the matrix->2:2
 Input the col of the matrix ->2:2

Input data for matrix-> 2
Input Value for: 1: 1: 2
Input Value for: 1: 2: 3
Input Value for: 2: 1: 4
Input Value for: 2: 2: 5

Entered Matrix First is:
2.000000 3.000000 4.000000
5.000000 6.000000 7.000000
1.000000 2.000000 4.000000
 
Entered Matrix Two is:
2.000000 3.000000
4.000000 5.000000

Multiplication is not possible

Explanation:

Step 1: The first matrix is read from the user using scanf() function.

Step 2: Similarly the second matrix is read from the user using scanf() function.

Step 3: The matrices are checked for condition whether they can be multiplied or not.

Step 4: If condition is true then they are multiplied using temprory array.

Step 5: The resulting matrix is printed on the output screen using printf() function.

More Insights:

1.More Information

Explore C Programs: C Programs