C program to perform multiplication operation on two matrix

 C program to perform multiplication operation on two matrix



/******************************************************

C program to perform multiplication operation on two matrix

************************************************************/

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

{

    int r1,r2,c1,c2,i,j,k, a[20][20],b[20][20],c[20][20];

    printf("How many rows and columns for first matrix\n");

    scanf("%d%d",&r1,&c1);

    printf("How many rows and columns for first matrix\n");

    scanf("%d%d",&r2,&c2);

    printf("Enter array elements of first array \n");

    if(c1!=r2)

    exit(0); // if condition true compiler will come out from input terminal

    for(i=0;i<r1;i++)

    {

        for(j=0;j<c1;j++)

        {

            scanf("%d",&a[i][j]);

        }

    }

    printf("Enter array elements of second array \n");

    for(i=0;i<r2;i++)

    {

        for(j=0;j<c2;j++)

        {

            scanf("%d",&b[i][j]);

        }

    }

    

    printf("First array is \n");

    for(i=0;i<r1;i++)

    {

        for(j=0;j<c1;j++)

        {

            printf("%d\t",a[i][j]);

        }

        printf("\n");

    }

    printf("Second array is \n");

    for(i=0;i<r2;i++)

    {

        for(j=0;j<c2;j++)

        {

            printf("%d\t",b[i][j]);

        }

        printf("\n");

    }

    printf("The multiplication of two matrices is \n");

    for(i=0;i<r1;i++)

    {

        for(j=0;j<c2;j++)

        {

             c[i][j]=0;

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

            {

               

                c[i][j]=c[i][j]+a[i][k]*b[k][j];

            }

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

        }

        printf("\n");

    }

}


How many rows and columns for first matrix

2 3

How many rows and columns for first matrix

3 2

Enter array elements of first array 

1 2 3 4 5 6

Enter array elements of second array 

1 2 3 4 5 6

First array is 

1 2 3

4 5 6

Second array is 

1 2

3 4

5 6

The multiplication of two matrices is 

22 28

49 64


टिप्पणी पोस्ट करा

0 टिप्पण्या