Subtraction of two matrices C++ Program

Simple C++ program to subtract one matrix from another matrix and store data into third matrix and then print third matrix.

/* C++ Program to subtract two matrix */
#include<iostream>
using namespace std;
int main()
{
	int m1[3][3], m2[3][3], m3[3][3];
 
	cout<<"Enter first matrix elements: "<<endl;
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			cin>>m1[i][j];
		}
	}
 
	cout<<"Enter second matrix elements: "<<endl;
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			cin>>m2[i][j];
		}
	}
 
    /* subtract m2 from m2 matrix and store data into m3 */ 
    for(int i=0;i<3;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		m3[i][j]=m1[i][j]-m2[i][j];
		}
	}
 
	/* print m3 matrix */
	cout<<"Subtraction of two matrices: "<<endl;
	for(int i=0;i<3;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		cout<<m3[i][j]<<"  ";
		}
		cout<<endl;
	}
 
	return 0;
}


Another C++ program without third matrix

/* C++ Program to subtract two matrix */
#include<iostream>
using namespace std;
int main()
{
	int m1[3][3], m2[3][3];
 
	cout<<"Enter first matrix elements: "<<endl;
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			cin>>m1[i][j];
		}
	}
 
	cout<<"Enter second matrix elements: "<<endl;
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			cin>>m2[i][j];
		}
	}
 
    /* subtract m2 from m2 matrix and store data into m1 */ 
    for(int i=0;i<3;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		m1[i][j]=m1[i][j]-m2[i][j];
		}
	}
 
	/* print m1 matrix */
	cout<<"Subtraction of two matrices: "<<endl;
	for(int i=0;i<3;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		cout<<m1[i][j]<<"  ";
		}
		cout<<endl;
	}
 
	return 0;
}


Popular posts from this blog