C++ Program to Multiply Two Matrices

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Multiplication of Two Matrix 
#include<iostream>
#define clrscr() system("cls")
using namespace std;

int main()
{
    int a[50][50],b[50][50],c1[50][50],r,c,k,m,n;
    clrscr();
    xyz:cout<<"\nEnter value of row and column\n";
    cin>>m>>n;
    if(m>50 || n>50)
    {
        cout<<"\n Invalid limit pls enter again\n";
        goto xyz;
    }
    else
    {
        // Read first matrix
         cout<<"\n Enter elements in first matrix\n";
         for(r=0;r<m;r++)
         {
            for(c=0;c<n;c++)
            {
                cin>>a[r][c];
            }
         }
         // Read second matrix
         cout<<"\n Enter elements in first matrix\n";
         for(r=0;r<m;r++)
         {
            for(c=0;c<n;c++)
            {
                cin>>b[r][c];
            }
         }
        // Print first matrix
        cout<<"\n Elements of first matrix\n";
         for(r=0;r<m;r++)
         {
            for(c=0;c<n;c++)
            {
                cout<<a[r][c]<<"\t";
            }
            cout<<"\n";
         }        
        // Print second matrix
       cout<<"\nElements of seconed matrix\n";
         for(r=0;r<m;r++)
         {
            for(c=0;c<n;c++)
            {
                cout<<b[r][c]<<"\t";
            }
            cout<<"\n";
         }  
       // Matrix Multiplication  
       for(r=0;r<m;r++)  
       {
          for(c=0;c<n;c++)
          {
            for(k=0;k<m;k++)
            {
                c1[r][c]=c1[r][c]+(a[r][k]*b[k][c]);
            }
          }
       }
      cout<<"\nResult of matrix multiplication\n";
         for(r=0;r<m;r++)
         {
            for(c=0;c<n;c++)
            {
                cout<<c1[r][c]<<"\t";
            }
            cout<<"\n";
         }  

    }
}

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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