The lower triangular matrix is a type of square matrix where all the elements above the principal diagonal are zero. To obtain the lower triangular matrix, the input matrix must be square, meaning it has an equal number of rows and columns. Typically, the dimensions of a square matrix are represented as n x n. By examining the elements above the principal diagonal and setting them to zero, we can display the lower triangular matrix in Java.
To convert a given matrix into a lower triangular matrix, we need to loop through the matrix and set the values of the elements to zero where the column number is greater than the row number. In our example, the principal diagonal elements of the matrix are located at positions (1, 1), (2, 2), and (3, 3), with values (1, 6, 6) respectively. All the elements above the diagonal, such as (1, 2), (1, 3), and (2, 3), need to be set to zero. By iterating through the matrix and applying the necessary conditions, we can effectively convert the given matrix into a lower triangular matrix.
Algorithm for a Java Program to Display the Lower Triangular Matrix
-
- STEP 1: START
- STEP 2: DEFINE rows, cols
- STEP 3: INITIALIZE matrix a[][] ={{1,2,3},{8, 6, 4}, {4, 5, 6}}
- STEP 4: rows = a.length
- STEP 5: cols = a[0].length
- STEP 6: if(rows!=cols)
then
PRINT “Matrix should be a square matrix”
else
Go to step 7 - STEP 7: REPEAT STEP 4 to STEP 6 UNTIL i<rows
//for(i=0; i<rows; i++) - STEP 8: REPEAT STEP 9 UNTIL j<cols // for(j=0; j<cols; j++)
If(j>i) then PRINT 0 else PRINT a[i][j] - STEP 9: PRINT new line
- STEP 10: END
Java Program to Display the Lower Triangular Matrix
public class LowerTriangular { public static void main(String[] args) { int rows, cols; //Initialize matrix a int a[][] = { {1, 2, 3}, {8, 6, 4}, {4, 5, 6} }; //Calculates number of rows and columns present in given matrix rows = a.length; cols = a[0].length; if(rows != cols){ System.out.println("Matrix should be a square matrix"); } else { //Performs required operation to convert given matrix into lower triangular matrix System.out.println("Lower triangular matrix: "); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(j > i) System.out.print("0 "); else System.out.print(a[i][j] + " "); } System.out.println(); } } } }
Output:
Lower triangular matrix: 1 0 0 8 6 0 4 5 6
In conclusion, the Java program to display the lower triangular matrix involves looping through the matrix and setting the elements above the principal diagonal to zero. By comparing the column number to the row number, we can identify and modify the appropriate elements. Follow tutorials.freshersnow.com to learn more.