C++ Program to Multiply Two Matrices

Hey Guys! Shall we start the discussion to learn the topic “C++ Program to Multiply Two Matrices” using this tutorial? Come on! Let’s try to do this task below.

C++ Program to Multiply Two Matrices

C++ Program to Multiply Two Matrices

Program Code

#include<iostream>
 
using namespace std;
 
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
cout<<"Enter rows and columns of first matrix:";
cin>>m>>n;
cout<<"Enter rows and columns of second matrix:";
cin>>p>>q;
 
if(n==p)
{
cout<<"nEnter first matrix:n";
for(i=0;i<m;++i)
for(j=0;j<n;++j)
cin>>a[i][j];
 
cout<<"nEnter second matrix:n";
for(i=0;i<p;++i)
for(j=0;j<q;++j)
cin>>b[i][j];
 
cout<<"nThe new matrix is:n";
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
cout<<c[i][j]<<" ";
}
cout<<"n";
}
}
else
cout<<"nSorry!!!! Matrix multiplication can't be done";
 
return 0;
}

Final Words

I hope this article helps you to know about “C++ Program to Multiply Two Matrices“. If you face any issues please let me know via the comment section. Share this article with other C/C++ program developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment