C Program To Find Sum of Lower Triangle Elements
Learn How To Find Sum of Lower Triangle Elements in C Programming Language. This C program to Calculate Sum of Lower Triangular Elements performs the addition of the elements in the Lower Triangle region of a matrix or a 3 – dimensional array.
A Triangular Matrix is a special kind of a Square Matrix. A square matrix is a lower triangular matrix if the elements above the main diagonal are zero.
The first method to calculate sum of lower triangular matrix elements makes use of Dynamic Memory Allocation and Pointers. The malloc() method is used to fetch the array elements. The condition to find lower triangular matrix elements is count1>count2. There is another simple method given below that makes use of Static Memory Allocation with arrays.
Also Read: C Program To Delete Vowels From A String
Method 1: Code To Find Sum of Lower Triangle Elements in C Programming using DMA
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 | #include<stdio.h> #include<malloc.h> int main() { int mat[5][5], limit, count1, count2, *ptr, sum; printf("\nEnter the Order of Matrix:\n"); scanf("%d", &limit); ptr = (int *) malloc (limit * sizeof(int)); printf("\nEnter %d Elements:\n", limit * limit); for(count1 = 1; count1 <= limit; count1++) { for(count2 = 1; count2 <= limit; count2++) { scanf("%d", ((ptr + count1) + count2)); } } sum = 0; for(count1 = 1; count1 < limit+1; ++count1) { for(count2 = 1; count2 < limit + 1; ++count2) { if(count1 > count2) { sum = sum+*((ptr + count1) + count2); } } } printf("\nSum of Lower Triangular Elements: %d\n",sum); printf("\n"); return 0; } |
Also Read: Difference between Call by Value and Call By Reference
Method 2: C Program To Calculate Addition of Lower Triangular Matrix Elements
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 | #include<stdio.h> int main() { int count1, column, mat[10][10], count2, sum, row; printf("\nEnter the Number of Columns:\n"); scanf("%d", &column); printf("\nEnter the Number of Rows:\n"); scanf("%d", &row); for (count1 = 0; count1 < row; count1++) { for (count2 = 0; count2 < column; count2++) { printf("\nEnter the Element: "); scanf("%d", &mat[count1][count2]); } } sum = 0; for (count1 = 0; count1 < row; count1++) { for (count2 = 0; count2 < column; count2++) { if (count1 > count2) { sum = sum + mat[count1][count2]; } } } printf("\nSum of Lower Triangular Elements of the Matrix: %d\n", sum); return 0; } |
Output

Also Read: C Program To Print Map of India
If you have any compilation error or doubt with this Code to Find Sum of Lower Triangle Elements in C Programming Language, mention about it in the comment section below.
nice..thank for informasi
Your Most Welcome Adi! 🙂