Matrix multiplication C program

It’s a matrix multiplication C program that inputs numbers from the user and stores these numbers in two different two-dimensional arrays and then multiplies them, and print result on the screen.

matrix_multiplication_c_program

To understand this program, it’s better you should know the following topics:

Program (1): to find multiplication of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
//consider matrices A , B and C size as 3x3
int a[3][3],b[3][3],d[3][3];
int i,j,k,m,n,q,p;
//accepting values into an array a
printf("Accept elements in Matrix A\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
       scanf("%d",&a[i][j]);
       }
 }
//accepting values into an array b
printf("Accept elements in Matrix B\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
       scanf("%d",&b[i][j]);
       }
 }
 //perform multiplication of array a and array b elements and store result in array c
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
       d[i][j]=0;
       for(k=0;k<3;k++)
       {
       d[i][j]=d[i][j]+a[i][k]*b[k][j];
       }
 }
 }
 //printing array c elements
printf("\n\nResultant matrix\n\n");
for(i=0;i<3;i++)
{
     for(j=0;j<3;j++)
      {
       printf("%6d",d[i][j]);
       }
       printf("\n");
 }
getch();
}

Output (1)

Accept elements in Matrix A
1
2
3
4
5
6
7
8
9
Accept elements in Matrix B
1
5
9
7
2
9
3
8
1


Resultant matrix

    24    33    30
    57    78    87
    90   123   144

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *