Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIJava Program to Display Lower Triangular Matrix

Java Program to Display Lower Triangular Matrix

Lower Triangular Matrix is a square matrix in which all the elements above the principal diagonal are 0. If the matrix is not a square matrix, it can never be called the lower triangular matrix.

Examples

Input 1: mat[][] = { {2, 1, 4},
                     {1, 2, 3},  
                     {3, 6, 2}}
                 
Output :  mat[][]= {{2, 0, 0},
                    {1, 2, 0},  
                    {3, 6, 2}}
                 
Explanation :    All the element below the principal diagonal needs to be 0.

Input 2 :  mat[][]=    {{4, 6},
                         {2, 8}}
               
Output :   mat[][]=     {{4, 0},
                         {2, 8}}

Input 3 :  mat[][] = { {2, 1, 4, 6},
                       {1, 2, 3, 7},  
                       {3, 6, 2, 8} }  
  
Output :   Matrix should be a Square Matrix

Approach:

  • If the matrix has equal rows and columns, continue the program else exit the program.
  • Run the loop over the whole matrix, and for the rows, whose column number is greater than row number, make the element at that position equal to 0.

Below is the implementation of the above approach: 

Java




// Java program for displaying lower triangular matrix
import java.io.*;
 
class GFG {
    static void lowerTriangularMatrix(int matrix[][])
    {
        int row = matrix.length;
        int col = matrix[0].length;
 
        // if number of rows and columns are not equal,
        // then return back
        if (row != col) {
            System.out.println(
                "Matrix should be a Square Matrix");
            return;
        }
        else {
            // looping over the whole matrix
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
 
                    // for the rows,whose column number is
                    // greater then row number,mark the
                    // element as 0
                    if (i < j) {
                        matrix[i][j] = 0;
                    }
                }
            }
 
            System.out.println(
                "Lower Triangular Matrix is given by :-");
            // printing the lower triangular matrix
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    System.out.print(matrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
    public static void main(String[] args)
    {
        // driver code
        int mat[][]
            = { { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } };
        // calling the function
        lowerTriangularMatrix(mat);
    }
}


Output:

Lower Triangular Matrix is given by :-
2 0 0 
1 2 0 
3 6 2 
  • Space Complexity: Matrix can be changed in-place. i.e no extra matrix space required.  0(N^2)
  • Time Complexity: 0(N^2)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments