on Leave a Comment

C++ Program to Add Two Matrices

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

C++ Program to Add Two Matrices

#include<iostream>
using namespace std;
int main()
{
        int a[3][3], b[3][3], c[3][3];
        int i, j;
        cout<<"Enter matrix 1 elements :\n";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cin>>a[i][j];
                }
        }
        cout<<"Enter matrix 2 elements :\n";
        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++)
                {
                        c[i][j]=a[i][j]+b[i][j];
                }
        }
        cout<<"\nSum of matrix 1 and matrix 2 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 matrix 1 elements :
1
1
1
1
1
1
1
1
1
Enter matrix 2 elements :
1
1
1
1
1
1
1
1
1

Sum of matrix 1 and matrix 2 is
2       2       2
2       2       2
2       2       2

0 comments:

Post a Comment