C++ program to find sum of lower triangular matrix
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#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