C++ Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

C++ Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

Write C++ Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

// CPP Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

#include <iostream>

using namespace std;

int main()
{
    int i,j ,sum1 = 0,sum2 = 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++)
		{
			if(i == j)
			{
				sum1 += A[i][i];
			}
			else
			{
				sum2 += A[i][j];
			}

		}
	}
	cout << "\n\nSum of Diagonal Elements :--> " << sum1;
	cout << "\nSum of Non Diagonal Elements :--> " << sum2;
	return 0;
}

Output:

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


Sum of Diagonal Elements :--> 1
Sum of Non Diagonal Elements :--> 27