Program to display the lower triangular matrix

Explanation

In this program, we need to display the lower triangular matrix.

Lower Triangular Matrix

Lower triangular matrix is a square matrix in which all the elements above the principle diagonal will be zero. To find the lower triangular matrix, a matrix needs to be a square matrix that is, number of rows and columns in the matrix needs to be equal. Dimensions of a typical square matrix can be represented by n x n.

Program to display the lower triangular matrix

Consider the above example, principle diagonal element of given matrix is (1, 6, 6). All the elements above diagonal needs to be made zero. In our example, those elements are at positions (1,2), (1,3) and (2,3). To convert given matrix into the lower triangular matrix, loop through the matrix and set the values of the element to zero where column number is greater than row number.

Algorithm

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the array and store it in variables rows and cols respectively.
  3. If the number of rows are not equal to the number of columns, then the given matrix is not a square matrix. Hence, given matrix cannot be converted to the lower triangular matrix. Display the error message.
  4. If rows = cols, traverse the array a using two loops where outer loop represents the rows, and inner loop represents the columns of the array a. To convert given matrix to lower triangular matrix, set the elements of the array to 0 where (j > i) that is, the column number is greater than row number.
  5. Display the resulting matrix.

Solution

Python

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

C

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

JAVA

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

C#

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

PHP

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

Next Topic#




Latest Courses