How to Print Lower Triangular Matrix in Java?


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

Triangular Matrix − A square matrix is called as a triangular matrix if all the elements above or below the diagonal of that matrix are zeros.

Lower- Triangular Matrix − A square matrix is called as a Lower- Triangular Matrix if all the elements above the diagonal of that matrix are zeros.

Here we have to print the lower triangular matrix.

Let’s start!

To show you some instances

Instance-1

Given matrix =

21 	22	23
24	25	26
27	28	29

Lower triangular matrix of given matrix is −

21 	0	0
24	25	0
27	28	29

Instance-2

Given matrix =

921 	222	243	432
124	745	256	657
237	258	429	345
176	453	756	843

Lower triangular matrix of given matrix is −

921 	0	0	0
124	745	0	0
237	258	429	0
176	453	756	843

Instance-3

Given matrix =

1 	2	3
4	5	6
7	8	9

Lower triangular matrix of given matrix is −

1 	0	0
4	5	0
7	8	9

Algorithm

Algorithm-1

  • Step-1 − Initialize a 2D array matrix to represent the input matrix.

  • Step-2 − Use two nested loops to iterate through each element of the matrix. The outer loop iterates through the rows of the matrix, and the inner loop iterates through the columns of each row.

  • Step-3 − Check if the current column is greater than the current row. If so, set the current element to 0.

  • Step-4 − Print the current element.

  • Step-5 − Move to the next line after the inner loop is finished to print the next row.

Algorithm-2

  • Step-1 − Initialize a 2D array matrix to represent the input matrix.

  • Step-2 − Use an IntStream to loop through the rows of the matrix.

  • Step-3 − Use another IntStream to loop through the columns of each row.

  • Step-4 − Check if the current column is greater than the current row. If so, set the current element to 0.

  • Step-5 − Print the current element. Move to the next line after the inner loop is finished to print the next row

Syntax

1. The Matrix.length() method in Java returns the length of the given matrix.

Below refers to the syntax of it −

inputMatrix.lenght

where, ‘inputMatrix’ refers to the given matrix.

2. IntStream.range() is a method in Java that generates a sequential stream of integers from startInclusive to endExclusive - 1.

IntStream.range(startInclusive, endExclusive)

It can be used to perform operations on a set of integers. For example, in the second code, it is used to loop through the rows and columns of the matrix.

The method forEach is then called on the stream to perform an action for each element in the stream.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Nested Loops

  • By Using Stream

Let’s see the program along with its output one by one.

Approach-1: By Using Nested Loops

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm by using nested loop method we print the lower triangular matrix of the given matrix.

Example

public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{11, 22, 33, 44},
         {55, 66, 77, 88},
         {99, 10, 11, 12},
         {13, 14, 15, 16}};
      LowerTriangularMatrix(inputMatrix);
   }
   public static void LowerTriangularMatrix(int[][] mat) {
	   System.out.println("Lower triangular matrix of given matrix is: ");
      
      // initiate the loop to check through the rows
      for (int a = 0; a < mat.length; a++) {
         
         // then check through each columns
         for (int b = 0; b < mat[a].length; b++) {
            
            // If the column is greater than the row, set the element to 0
            if (b > a) {
               mat[a][b] = 0;
            }
            
            // Print the current element
            System.out.print(mat[a][b] + " ");
         }
         System.out.println();
      }
   }
}

Output

Lower triangular matrix of given matrix is: 
11 0 0 0 
55 66 0 0 
99 10 11 0 
13 14 15 16

Approach-2: By Using Stream

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm by using stream method we print the lower triangular matrix of the given matrix.

Example

import java.util.stream.IntStream;
public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{12, 32, 13},
         {5, 6, 7},
         {9, 10, 11}};
      
      //call the user-defined method
      lowerTriangularMatrix(inputMatrix);
   }
   public static void lowerTriangularMatrix(int[][] mat) {
      System.out.println("Lower triangular matrix of given matrix is: ");
      
      // Use IntStream to loop through the rows of the matrix
      IntStream.range(0, mat.length)
      .forEach(a -> {
         
         // Use IntStream to loop through the columns of the current row
         IntStream.range(0, mat[a].length)
         .forEach(b -> {
            
            // If the column is greater than the row, set the element to 0
            if (b > a) {
               mat[a][b] = 0;
            }
            
            // Print the current element
            System.out.print(mat[a][b] + " ");
         });
         System.out.println();
      });
   }
}

Output

Lower triangular matrix of given matrix is: 
12 0 0 
5 6 0 
9 10 11

In this article, we explored different approaches to print lower triangular by using Java programming language.

Updated on: 04-May-2023

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements