- Home
- Course Category
- Course Result
- Course Detail
Hot Line: 0124-2383121
Course Introduction
Programs for practice
To add two matrices in C++ Programming, you have to ask to the user to enter the elements of both the matrix, now start adding the two matrix to form a new matrix. After adding two matrices display the third matrix which is the result of the addition of the two matrices as shown in the following program.
Source Code: Add Two Matrices
/* C++ Program - Add Two Matrices */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
cout<<"Enter matrix 1 elements :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter matrix 2 elements :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat2[i][j];
}
}
cout<<"Adding the two matrix to form the third matrix .....\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
cout<<"The two matrix added successfully...!!";
cout<<"The new matrix will be :\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"\n";
}
getch();
}
Output
Enter matrix 1 elements: 1 1 1 1 1 1 1 1 1
Enter matrix 2 elements:2 2 2 2 2 2 2 2 2
Adding the two matrix to form the third matrix....
The two matrix added sucessfully!! The new matrix will be:
3 3 3
3 3 3
3 3 3