C program to find sum of lower triangular matrix

Write a C program to read elements in a matrix and find sum of lower triangular matrix. How to find sum of lower triangular matrix in C. Logic to find sum of lower triangular matrix in C programming.

Example

Input

Input elements in matrix:
1 0 0
4 5 0
7 8 9

Output

Sum of lower triangular matrix = 19

Required knowledge

Basic C programming, For loop, Array

Must know – Program to find lower triangular matrix

Lower triangular matrix

Lower triangular matrix is a special square matrix whole all elements above the main diagonal is zero.

Lower triangular matrix

Logic to find sum of lower triangular matrix

To find sum of lower triangular matrix, we need to find the sum of elements marked in the red triangular area.

Lower triangular matrix elements

For any matrix A sum of lower triangular matrix elements is defined as –
sum = sum + Aij (Where j < i).

Read more – Program to find sum of upper triangular matrix

Program to find sum of lower triangular matrix

/**
 * C program to find sum of lower triangular matrix
 */

#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3

int main()
{
    int A[MAX_ROWS][MAX_COLS];
    int row, col, sum = 0;

    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS);
    for(row=0; row<MAX_ROWS; row++)
    {
        for(col=0; col<MAX_COLS; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Find sum of lower triangular matrix */
    for(row=0; row<MAX_ROWS; row++)
    {
        for(col=0; col<MAX_COLS; col++)
        {
            if(col<row)
            {
                sum += A[row][col];
            }
        }
    }

    printf("Sum of lower triangular matrix = %d", sum);

    return 0;
}

Output

Enter elements in matrix of size 3x3:
1 0 0
4 5 0
7 8 9
Sum of lower triangular matrix = 19

Happy coding 😉