Dark Mode On/Off

# C Program To Find Sum of lower triangular matrix

A matrix in which all the elements above the main diagonal are zero is known as a lower triangular matrix. Here, we are given a matrix and we have to calculate the sum of all the elements in the lower triangular matrix. But before moving forward if you are not familiar with the concept of the array in C, then do check the article on Arrays in C.

Below is the pictorial representation of the lower triangular matrix. Input: Enter the matrix elements:

1 2 3

4 5 6

7 8 9

Output: Sum of the lower triangular matrix: 26

Explanation: Here, the lower triangle elements are 1 4 5 7 8 9. These elements add up to a sum of 34.

## Program 1: To Find the Sum of Lower Triangular Matrix

In this progam, firstly we will declare a 2d array and then initialize it. After that, the sum of all the elements in the lower triangular matrix is calculated directly.

### Algorithm

1. Start
2. Declare an M*N 2D array.
3. Initialize the array.
4. If the number of rows and columns are not equal then it is not possible to display the sum of the elements of the lower triangle.
5. If the number of rows is equal to the number of columns, then proceed for the calculation of lower triangular elements.
6. Using two for loops traverse through the elements.
7. If (i>=j), then add all the elements.
8. Display the final result.
9. Stop.

In the below program, we have to find the sum of all the elements in the lower triangular matrix directly.

``````#include <stdio.h>
int main()
{
int m,n;
printf("Enter the number of rows and column: \n");
scanf("%d %d",&m,&n);     //Matrix size declaration
int a[m][n];
printf("\nEnter the elements of the matrix: \n");
for(int i=0;i<m;i++)     //Matrix Elements initialization
{
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe elements in the matrix are: \n");
for(int i=0;i<m;i++)     //Print the elements in the matrix
{
for(int j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
int lsum=0;        //Declare a variable to calculate lower triangular sum
if(m==n)          //Check whether number of rows and column are equal or not
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i>=j)     //Iterate only through the lower triangular elements
lsum=lsum+a[i][j];   //Calculate the sum
}
}
printf("\nThe sum of lower triangular matrix is %d",lsum);   //Display the sum
}
else
{
printf("Not Possible to display lower triangular elements sum");   //if number of rows and column are not equal
}
return 0;
}``````

Enter the number of rows and column: 3 3

Enter the elements of the matrix: 1 2 3 4 5 6 7 8 9

The elements in the matrix are:
1 2 3
4 5 6
7 8 9

The sum of lower triangular matrix is 34

## Program 2: To Find the Sum of Lower Triangular Matrix

In this progam, we use a function to display the sum of the lower triangle elements in the matrix. Here, firstly we declare and initialize the 2d matrix and then call the function to calculate the sum of the lower triangular elements.

### Algorithm

1. Start
2. Declare an M*N 2D array.
3. Initialize the array.
4. If the number of rows and columns are not equal then it is not possible to display the sum of the elements of the lower triangle.
5. If the number of rows is equal to the number of columns, then proceed for the calculation of lower triangular elements.
6. Now call a function that will display the sum of lower triangle elements in the matrix.
7. Using two for loops traverse through the elements.
8. If (i>=j), then add all the elements.
9. Display the final result.
10. Stop.

In this program, the sum of the lower triangular matrix is calculated by using functions. Here, after declaring and initializing the matrix a function is called which will add all the elements in the lower triangle.

``````#include <stdio.h>
void lsum(int arr, int m, int n);     //Function Declaration
int main()
{
int m,n;                 //Matrix Size Declaration
printf("Enter the number of rows and column: \n");
scanf("%d %d",&m,&n);   //Matrix Size Initialization
int arr;        //Matrix Size Declaration
printf("\nEnter the elements of the matrix: \n");
for(int i=0;i<m;i++)    //Matrix Initialization
{
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nThe elements in the matrix are: \n");
for(int i=0;i<m;i++)     //Print the matrix
{
for(int j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
if(m==n)               //If number of rows and columns equal
{
lsum(arr,m,n);      //Call the function
}
else
{                   //Not possible to declare lower triangular elements
printf("Not Possible to display lower triangular elements sum");
}
return 0;
}
void lsum(int arr, int m, int n)    //Function Definition
{
int llsum=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i>=j)          //Traverse only in the lower triangle
llsum=llsum+arr[i][j];    //Add the elements
}
}
//Print the sum of lower triangular elements
printf("\nThe sum of lower triangular matrix is %d",llsum);
}``````

Enter the number of rows and column: 3 3

Enter the elements of the matrix: 1 2 3 4 5 6 7 8 9

The elements in the matrix are:
1 2 3
4 5 6
7 8 9

The sum of lower triangular matrix is 34