on Leave a Comment

C++ Program to Multiply Two Matrices

This article contains C++ programming code to multiply two matrices.

C++ Program to Multiply Two Matrices

#include<iostream>
using namespace std;
int main()
{
        int a[3][3], b[3][3], c[3][3], sum=0, i, j, k;
        cout<<"Enter elements of matrix 1 : ";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cin>>a[i][j];
                }
        }
        cout<<"Enter elements of matrix 2 : ";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cin>>b[i][j];
                }
        }
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        sum=0;
                        for(k=0; k<3; k++)
                        {
                                sum = sum + a[i][k] * b[k][j];
                        }
                        c[i][j] = sum;
                }
        }
        cout<<"\nMultiplication of two Matrices is \n";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cout<<c[i][j]<<"\t";
                }
                cout<<"\n";
        }
        return 0;
}

OUTPUT:

Enter elements of matrix 1 : 1
2
3
4
5
6
7
8
9
Enter elements of matrix 2 : 1
2
3
4
5
6
7
8
9

Multiplication of two Matrices is
30      36      42
66      81      96
102     126     150

See also:


0 comments:

Post a Comment