codescracker


c++

C++ Program to Add Two Matrices



« Previous Program Next Program »


Add two Matrices in C++

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.

C++ Programming Code to Add Two Matrices

Following C++ program add two 3*3 matrices to form the third matrix :

/* 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();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program to add two matrices

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test