C++ Program to Find Row Sum and Column Sum of Matrix

Write C++ Program to Find Row Sum and Column Sum of Matrix

// CPP Program to Find Row Sum and Column Sum of Matrix

#include <iostream>

using namespace std;

int main()
{
	int i,j ,sum = 0;
	int A[3][3] = { {1, 2, 0}, {4, 0, 6}, {7, 8, 0} };

	cout << "Matrix A :--> \n";
	for(i = 0;i < 3; i++)
	{
		for( j = 0; j < 3; j++)
		{
			cout << A[i][j] << " ";
		}
		cout << endl;
	}

	for(i = 0; i < 3; i++)
	{
		for(j = 0; j < 3; j++)
		{
			sum = sum + A[i][j];
		}
		cout << "\nSum of row " << i << " :--> " << sum;
		sum = 0;
	}

	printf("\n");
	for(i = 0;i < 3; i++)
	{
		for(j = 0;j < 3; j++)
		{
			sum = sum + A[j][i];
		}
		cout << "\nSum of Column " << i << " :--> " << sum;
	}

	return 0;
}

Output:

Matrix A :-->
1  2  0
4  0  6
7  8  0

Sum of row 0 :--> 3
Sum of row 1 :--> 10
Sum of row 2 :--> 15

Sum of Column 0 :--> 12
Sum of Column 1 :--> 10
Sum of Column 2 :--> 6

≪ Previous | Next ≫