Java Program to display Matrix Lower Triangle

Write a Java Program to display Matrix Lower Triangle with an example. A Java Matrix lower triangle is a square matrix whose items above the diagonal are zeros.

In this Java Matrix Lower Triangle example, we declared an integer matrix. Next, we used for loop to iterate the two-dimensional array. Within the for loop, we used the If statement to check whether the rows index position is greater than or equals to the columns index position. Suppose it is true, the compiler prints the Matrix original value. Otherwise, it prints the value 0’s.

public class MatrixLowerTriangle {
	
	public static void main(String[] args) {
		
		int i, j;
		
		int[][] Lt_arr = {{11, 21, 31}, {41, 51, 61}, {71, 81, 91}};
		
		System.out.println("\n---The Lower Triangle of the given Matrix---");
		for(i = 0; i < 3 ; i++)
		{
			System.out.print("\n");
			for(j = 0; j < 3; j++)
			{
				if(i >= j) {
					System.out.format("%d \t", Lt_arr[i][j]);
				}
				else {
					System.out.print("0 \t");	
				}
			}
		}
	}
}
Java Program to display Matrix Lower Triangle 1

Java Program to display Matrix Lower Triangle example 2

This Java Lower Triangle of a Matrix code is the same as the above. However, this Java code allows the user to enter the number of rows, columns, and the matrix items.

import java.util.Scanner;

public class MatrixLowerTriangle {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int i, j, rows, columns;
		
		sc= new Scanner(System.in);
		
		System.out.println("\n Please Enter LT Matrix Rows and Columns :  ");
		rows = sc.nextInt();
		columns = sc.nextInt();
		
		int[][] arr = new int[rows][columns];
		
		System.out.println("\n Please Enter the LT Matrix Items :  ");
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				arr[i][j] = sc.nextInt();
			}		
		}
		System.out.println("\n---The Lower Triangle of the given Matrix---");
		for(i = 0; i < rows ; i++)
		{
			System.out.print("\n");
			for(j = 0; j < columns; j++)
			{
				if(i >= j) {
					System.out.format("%d \t", arr[i][j]);
				}
				else {
					System.out.print("0 \t");	
				}
			}
		}
	}
}

Java Matrix Lower Triangle output

 Please Enter LT Matrix Rows and Columns :  
3 3

 Please Enter the LT Matrix Items :  
10 20 99
33 55 88
77 89 44

---The Lower Triangle of the given Matrix---

10 	0 	0 	
33 	55 	0 	
77 	89 	44