Q:

Write a C++ Program to Find Sum Above and Below of Main Diagonal Matrix

belongs to collection: C++ Arrays Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 :

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Arrays Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Menu Driven Program for Queue Operations using... >>
<< Write a C++ Program to Print Lowerhalf and Upperha...