Write a C++ program to find the sum of a lower triangular matrix?
How to find the sum of a lower triangular matrix (2D- 2 Dimensional Array) in C++. Logic to find the sum of the lower triangular matrix in C++ programming.
Write a C++ program to read elements in a matrix(2D- 2 Dimensional Array) and find the sum of a lower triangular matrix.
Logic
Program
#include<iostream>
using namespace std;
#define max_rows 3
#define max_cols 3
int main()
{
int a[max_rows][max_cols];
int row,col,sum=0;
cout<<"Please enter elements of matrix of given size"<<max_rows<<"x"<<max_cols<<endl;
for(row=0; row<max_rows ;row++)
{
for(col=0;col<max_cols;col++)
{
cin>>a[row][col];
}
}
for(row=0; row<max_rows ;row++)
{
for(col=0;col<max_cols;col++)
{
cout<<a[row][col];
}
cout<<endl;
}
for(row=0;row<max_rows;row++)
{
for(col=0;col<max_cols;col++)
{
if(col<row)
{
sum+=a[row][col];
}
}
}
cout<<"sum of lower triangular matrix="<<sum;
}
Output


