Here's a C Program to find the sum of lower triangular elements of a matrix with explanation and example. This program uses Multidimensional Arrays, Nested Loops and For Loops.
Note: A lower triangular matrix is a square matrix in which the elements above the main diagonal are zero.
# include <stdio.h> # include <conio.h> void main() { int mat[10][10] ; int i, j, size, lower = 0 ; clrscr() ; printf("Enter size of the square matrix : ") ; scanf("%d", &size) ; printf("\nEnter the elements of the matrix : \n\n") ; for(i = 0 ; i < size ; i++) for(j = 0 ; j < size ; j++) scanf("%d", &mat[i][j]) ; printf("\nThe lower triangular matrix is : \n\n") ; for(i = 0 ; i < size ; i++) { for(j = 0 ; j < size ; j++) { if(j <= i) { printf("%d\t", mat[i][j]) ; lower = lower + mat[i][j] ; } else printf("\t") ; } printf("\n") ; } printf("\nThe sum of lower triangular elements is : %d", lower) ; getch() ; }
Output of the above program
Enter size of the square matrix : 3
Enter the elements of the matrix :
1 2 3
4 5 6
7 8 9
The lower triangular matrix is :
1
4 5
7 8 9
The sum of lower triangular elements is : 34
Explanation of above program
In this program, we have a square matrix mat
of maximum size 10 x 10. The variables i and j are the loop variables
that corresponds to the row and column of the matrix respectively. The
variable size contains the size of the matrix.
First
the program asks the user to enter the size of the matrix and then
using a nested for loops the matrix is populated. Now to get the lower triangular matrix, the program prints only those values in the matrix
for which j <= i or column number is less than or equal to row number. Also the sum of lower triangular elements is also calculated
and stored in the variable lower.
Also take a look at: C Program to Find the Sum of Upper Triangular Matrix Elements
Hello There,
I learnt so much in such little time about C Program to Find the Sum of Lower Triangular Even a toddler could become smart reading of your amazing articles.
I am using 3 vectors each of which is of type struct ""ABC"". I am trying to find duplicates in each vector using equal range() method. In order for equal range() to work, the vectors must be sorted. The criteria for sorting each vector is different, therefore, I can't write a single sort method inside the struct ""ABC"". Is there any way to write different methods for sorting, one for each vector?
Appreciate your effort for making such useful blogs and helping the community.
Best Regards,
Nithya