Example:
If the elements of matrix are:
1 0 0
4 5 0
7 8 9
Sum of lower triangular matrix = 19
Required knowledge:
Basic C programming, For loop, Array, MatrixLower triangular matrix
Lower triangular matrix is a special square matrix whole all elements above the main diagonal is zero.Algorithm to find sum of lower triangular matrix:
Lower triangular matrix elements is shown in the below image. We need to find sum of all elements in the red triangular area.For any matrix A sum of lower triangular matrix elements can be defined as:
sum = sum + Aij (Where j < i).
Program:
/**
* C program to find sum of lower triangular matrix
*/
#include <stdio.h>
int main()
{
int A[3][3];
int row, col, sum = 0;
/*
* Reads elements in matrix from user
*/
printf("Enter elements in matrix of size 3x3: \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &A[row][col]);
}
}
/*
* Finds sum of lower triangular matrix
*/
for(row=0; row<3; row++)
{
for(col=0; col<3; 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
1 0 0
4 5 0
7 8 9
Sum of lower triangular matrix = 19
Happy coding ;)
You may also like
- Array and Matrix programming exercises index.
- C program to find sum of upper triangular matrix.
- C program to find sum of two matrices.
- C program to find sum of main diagonal elements of a matrix.
- C program to find sum of opposite diagonal elements of a matrix.
- C program to find sum of each row and columns of a matrix.
- C program to find sum of all elements of an array.
- C program to find sum of all even numbers between 1 to n.
- C program to find sum of all odd numbers between 1 to n.
- C program to find sum of first and last digit of a number.
- C program to find sum of all Prime numbers between 1 to n.
- C program to find transpose of a matrix.
- C program to check Symmetric matrix.
- C program to find reverse of a number.
- C program to find reverse of an array.