Program to evaluate Sum of Elements Above and Below Main Diagonal of Matrix in CPP (C plus plus)

Program to evaluate the Sum of Elements Above and Below Main Diagonal of Matrix in CPP (C plus plus)

In this tutorial, we will learn about Program to evaluate Sum of Elements Above and Below Main Diagonal of Matrix in CPP (C plus plus).

#include<iostream>
 
using namespace std;
 
int main()
{
    int array[5][5];
    int i;
	int j;
	int k;	
	int p=0;
	int q=0;
	
    cout<<"Please enter the size of matrix [MAX 5]:"<<endl;
    cin>>k;
    
    cout<<"Please enter the matrix:"<<endl;
    
    for(i=0;i<k;++i)
        for(j=0;j<k;++j)
            cin>>array[i][j];
    
    for(i=0;i<k;++i)
        for(j=0;j<k;++j)
            if(j>i)
                p+=array[i][j];
            else
                if(i>j)
                    q+=array[i][j];
    
    cout<<"Sum of values above the diagonal:"<<p<<endl;
    cout<<"Sum of values below the diagonal:"<<q<<endl;
 
    return 0;
}

Output:

Program to evaluate Sum of Elements Above and Below Main Diagonal of Matrix in CPP (C plus plus)
Program to evaluate the Sum of Elements Above and Below Main Diagonal of Matrix in CPP (C plus plus)

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.