Thursday, 15 August 2019

C programming for multiplication of two matrices

/*Multiplication of matrices*/


#include<stdio.h>
void
main()
{
            int i,j,m,n,p,k,a[20][20],b[20][20],c[20][20];
            printf("enter the order of matrix A:");
            scanf("%d%d",&m,&n);
                        printf("enter the order of matrix B:");
            scanf("%d%d",&n,&p);
            printf("enter the elements of matrix A:");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                scanf("%d",&a[i][j]);
                                    printf("\n");
                        }
                        printf("the input matrix A is:\n");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<n;j++)
                                                printf("%d\t",a[i][j]);
                                    printf("\n");
                        }
                        printf("enter the elements of matrix B :");
                        for (i=0;i<n;i++)
                        {
                                    for(j=0;j<p;j++)
                                                scanf("%d",&b[i][j]);
                                    printf("\n");
                        }
                        printf("the input matrix B is:\n");
                        for(i=0;i<n;i++)
                        {
                                    for(j=0;j<p;j++)
                                                printf("%d\t",b[i][j]);
                                    printf("\n");
                        }
                        /*matrix multiplication*/
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<p;j++)
                                    {
                                                c[i][j]=0;
                                                for(k=0;k<n;k++)
                                                {
                                                            c[i][j]=c[i][j]+a[i][k]*b[k][j];
                                                }
                                    }
                        }
                        printf("multiplication of the matrix is:\n");
                        for(i=0;i<m;i++)
                        {
                                    for(j=0;j<p;j++)
                                                printf("%d\t",c[i][j]);
                                    printf("\n");
                        }
}

No comments:

Post a Comment

Thanks for comment.