Write a C++ Program to Find Sum Above and Below of Main Diagonal Matrix. Here’s simple Program to Find Sum Above and Below of Main Diagonal Matrix in C Programming Language.
Main Diagonal of a matrix consists of the elements of a square from the upper left element proceeding to the down right element diagonally.
More About Main Diagonal :
The sum of the elements of the main diagonal of a matrix is called Trace of the matrix.
Examples of Main Diagonal :
The elements 1, 5, and 9 are the elements of the main diagonal of a 3 × 3 matrix.
Below is the source code for C++ Program to Find Sum Above and Below of Main Diagonal Matrix which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to Find Sum Above and Below of Main Diagonal Matrix */
#include<iostream>
using namespace std;
int main()
{
int arr[5][5],a=0,b=0,i,j,n;
cout<<"Enter size of matrix(max 5):";
cin>>n;
cout<<"Enter the matrix:\n";
for(i=0;i<n;++i)
for(j=0;j<n;++j)
cin>>arr[i][j];
for(i=0;i<n;++i)
for(j=0;j<n;++j)
if(j>i)
a+=arr[i][j];
else
if(i>j)
b+=arr[i][j];
cout<<"\nSum of elements above the diagonal:"<<a;
cout<<"\nSum of elements below the diagonal:"<<b;
return 0;
}
OUTPUT : :
*************OUTPUT*****************
/* C++ Program to Find Sum Above and Below of Main Diagonal Matrix */
Enter size of matrix(max 5):3
Enter the matrix:
1
2
3
4
5
6
7
8
9
Sum of elements above the diagonal:11
Sum of elements below the diagonal:19
Definition Of Main Diagonal : :
Main Diagonal of a matrix consists of the elements of a square from the upper left element proceeding to the down right element diagonally.
More About Main Diagonal :
Examples of Main Diagonal :
The elements 1, 5, and 9 are the elements of the main diagonal of a 3 × 3 matrix.
Below is the source code for C++ Program to Find Sum Above and Below of Main Diagonal Matrix which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
need an explanation for this answer? contact us directly to get an explanation for this answer