Saturday, December 4, 2021
HomeTop Coding QuestionWrite a program to multiply two matrix.

Write a program to multiply two matrix.

Given two matrix we have to calculate the multiplication of two matrix.

Multiplication means we have to perform A*B operation.

Example:

 input:  
A:1 2
  3 4
B:5 6
  7 8
Output:
         19 22 
         43 50
#include<bits/stdc++.h>
using namespace std;
int main(){  
int matrix1[10][10],matrix2[10][10],mul[10][10];
int row,col,i,j,k;    
cout<<"enter the number of row="<<endl;    
cin>>row;
cout<<"enter the number of column="<<endl;;    
cin>>col;
cout<<"enter the first matrix element=\n"<<endl;;    
for(i=0;i<row;i++)    
{    
for(j=0;j<col;j++)    
{    
cin>>matrix1[i][j];    
}    
}    
cout<<"enter the second matrix element=\n"<<endl;;    
for(i=0;i<row;i++)    
{    
for(j=0;j<col;j++)    
{    
cin>>matrix2[i][j];    
}    
}    
cout<<"multiplication result "<<endl;   
for(i=0;i<row;i++)    
{    
for(j=0;j<col;j++)    
{    
mul[i][j]=0;    
for(k=0;k<col;k++)    
{    
mul[i][j]+=matrix1[i][k]*matrix2[k][j];    
}    
}    
}    
for(i=0;i<row;i++)    
{    
for(j=0;j<col;j++)    
{    
cout<<mul[i][j]<<" ";  
}    
cout<<endl;    
}    
return 0;  
}  
multiplication result 
19 43
22 50


Time Complexity-O(N*N)

space Complexity-O(N*N);

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments