# WAP in C++ & Python that finds the sum of elements above and below the main diagonal of a Matrix

By | August 6, 2019

Here in this program, we will code to find out the sum of non-diagonal elements and separate them with elements above the diagonal and elements below the diagonal. Here in this article, we will consider only one diagonal the left to right one.

## Sum of elements above and below the main diagonal of a Matrix

For example, if the user enters the matrix-like

3  4  5

6  7  9

5  4  6

So the sum of the above diagonal elements will be 4+5+9 =18, and the sum of the below diagonal elements will be 6+5+4 = 15

### C++

```#include<iostream.h>
#include< conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
cout<<"Enter the elements in the array:"<<endl;
for(int i=0; i<3;i++)
{
for(int j=0; j<3;j++)
{cin>>arr[i][j];
if(j>i)
else
if(i>j)
bd+=arr[i][j];
}
}
cout<<"The sum of above diagonal elements is: "<<ad<<endl;
cout<<"The sum of below diagonal elements is: "<<bd;
getch();
}```

Output:

```Enter the elements in the array:

3
4
5
6
7
9
5
4
6

The sum of above diagonal elements is: 18
The sum of below diagonal elements is: 15```

### Python

```arr=[[0,0,0],[0,0,0],[0,0,0]]
bd=0
print("Enter the elements in the array:")
for i in range(3):
for j in range(3):
arr[i][j] = int(input())
if j>i:
else:
if i>j:
bd+=arr[i][j]
print("The sum of above diagonal elements is:",ad)
print("The sum of below diagonal elements is:",bd)```

Output:

```Enter the elements in the array:

3
4
5
6
7
9
5
4
6
The sum of above diagonal elements is: 18
The sum of below diagonal elements is: 15```