Google News
logo
Java Program to display the Lower Triangular Matrix
In the following examples of Java program to display the lower triangular matrix of a given square matrix :
Program :
public class LowerTriangularMatrix {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        printLowerTriangularMatrix(matrix);
    }

    public static void printLowerTriangularMatrix(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Output :
1 
4 5 
7 8 9 
In this program, we have a square matrix matrix represented as a two-dimensional array. We define a method printLowerTriangularMatrix that takes the matrix as input and prints its lower triangular matrix to the console.

We first calculate the size of the matrix using its length. We then use two nested loops to iterate over the elements of the matrix. The outer loop iterates over the rows, and the inner loop iterates over the columns up to the current row index.

We then print each element of the lower triangular matrix followed by a space. At the end of each row, we print a newline character to move to the next line.