#include <iostream>
using namespace std; 
class matmul
{
    public :
    int a[30][30],b[30][30],c[30][30],k, r1,r2,l,c1,c2,i,j;
    void getdata ();
    void display ();
    void mul();
};
void matmul::getdata()
{
    cout <<"Enter the row size for first matrix : ";
    cin>>r1;
    cout <<r1<<endl; 
    cout <<"Enter the column size for first matrix: ";
    cin >>c1;
    cout <<c1<<endl;
     cout <<"Enter the row size for second matrix : ";
    cin>>r2;
    cout <<r2<<endl; 
    cout <<"Enter the column size for second  matrix: ";
    cin >>c2;
    cout <<c2<<endl;
    if (c1==r2)
    {
    cout <<"Enter the elements for first matrix:"<<endl; 
    for (i=0;i<r1;i++)
    {
        for (j=0;j<c1;j++)
        {
            cin >>a[i][j];
        }
    }
    cout <<"Enter the elements for second matrix:"<<endl; 
    for (i=0;i<r2;i++)
    {
        for (j=0;j<c2;j++)
        {
            cin >>b[i][j];
        }
    }
    }
    else 
    {
    cout <<"matrix multiplication is not performed "<<endl; 
    cout <<"Note : column size of first matrix and row size of second matrix is must be equal "<<endl; 
   }
}
void matmul::display()
{
    if (c1==r2)
    {
    cout <<"First matrix is "<<endl; 
    for (i=0;i<r1;i++)
    {
        cout <<endl; 
        for (j=0;j<c1;j++)
        {
            cout <<"\t";
            cout <<a[i][j];
        }
    }
    cout <<"\n second  matrix is "<<endl; 
    for (i=0;i<r2;i++)
    {
        cout <<endl; 
        for (j=0;j<c2;j++)
        {
            cout <<"\t";
            cout <<b[i][j];
        }
    }
    }
}
void matmul::mul()
{
    if (c1==r2)
    {
    cout <<"\n Resultant matrix is :"<<endl; 
    for (i=0;i<r1;i++)
    {
        cout <<endl; 
        for (j=0;j<c2;j++)
        {
            c[i][j]=0;
            for (k=0;k<r2;k++)
            {
                c[i][j]=c[i][j]+a[i][k]*b[k][j];
            }
            cout <<"\t";
            cout <<c[i][j];
        }
    }
    }
}
int main() {
    matmul m; 
    cout <<"Multiplication n of two matrices "<<endl; 
    m. getdata();
    m. display ();
    m. mul();
    return 0;
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: