Java Program To Display Lower Triangular Matrix
In this tutorial,we will learn how to display the lower triangular matrix. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Below is the pictorial representation for the same.
Input: Enter the Matrix Elements:
1 2 3
4 5 6
7 8 9
Output: The lower triangular matrix is:
1 0 0
4 5 0
7 8 9
Two Cases arises for the above scenario:
Case 1: When values are User-defined
Case 2: When values are Pre-defined
Let us take a look at each of these cases separately.
Program 1: To Display the Lower Triangular Matrix
In the below program we will see how to display the lower triangular matrix when values are user-defined.
Algorithm:
- Start
- Declare variables to store the number of rows and columns.
- Ask the user to initialize the rows and columns.
- Check if the number of rows and columns are equal or not.
- If not equal, then display a message that the number of rows and columns should be equal.
- If equal, then declare a matrix.
- Ask the user to initialize the matrix elements.
- Print the original matrix.
- Call a method to display the lower triangular matrix.
- Use a loop to iterate over the elements.
- Assign 0 to the elements whose row's number is lesser than the column's number.
- Print the resultant matrix.
- Stop.
Below is the code for the same.
// Java Program to print the lower triangular matrix
import java.util.*;
public class Main
{
// Print the matrix
public static void printMatrix(int[][] arr)
{
int m = arr.length; //For Rows
int n = arr[0].length; //For columns
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
//Display the lower triangular matrix
public static void lowerTriangularMatrix(int arr[][])
{
int m = arr.length;
int n = arr[0].length;
if (m != n)
{
System.out.println("Matrix entered should be a Square Matrix");
System.out.println("Try Again..");
return;
}
else
{
// looping over the whole matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i < j)
{
arr[i][j] = 0;
}
}
}
System.out.println( "Lower Triangular Matrix is : ");
// printing the lower triangular matrix
printMatrix(arr);
}
}
public static void main(String[] args)
{
//Take input from the user
Scanner sc=new Scanner(System.in);
int m,n; //Declare variables for rows and columns
System.out.println("Enter the number of rows: ");
m=sc.nextInt();
System.out.println("Enter the number of columns: ");
n=sc.nextInt();
System.out.println("Enter the Matrix Elements: ");
int arr[][] = new int[m][n]; //Declare the matrix
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt(); //Initialize the matrix
}
}
//Print Original Matrix
System.out.println( "Original Matrix is : ");
printMatrix(arr);
// calling to display the lower triangular matrix
lowerTriangularMatrix(arr);
}
}
Enter the number of rows: 3
Enter the number of columns: 3
Enter the Matrix Elements: 1 2 8 7 6 5 4 3 9
Original Matrix is :
1 2 8
7 6 5
4 3 9
Lower Triangular Matrix is :
1 0 0
7 6 0
4 3 9
Program 2: To Display the Lower Triangular Matrix
In the below program we will see how to display the lower triangular matrix when values are pre-defined.
Algorithm:
- Start
- Declare variables to store the number of rows and columns.
- Initialize the rows and columns.
- Check if the number of rows and columns are equal or not.
- If not equal, then display a message that the number of rows and columns should be equal.
- If equal, then declare a matrix.
- Initialize the matrix elements.
- Print the original matrix.
- Call a method to display the lower triangular matrix.
- Use a loop to iterate over the elements.
- Assign 0 to the elements whose row's number is lesser than the column's number.
- Print the resultant matrix.
- Stop.
Below is the code for the same.
// Java Program to print the lower triangular matrix
import java.io.*;
public class Main
{
// Print the matrix
public static void printMatrix(int[][] arr)
{
int m = arr.length; //For Rows
int n = arr[0].length; //For columns
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
//Display the lower triangular matrix
public static void lowerTriangularMatrix(int arr[][])
{
int m = arr.length;
int n = arr[0].length;
if (m != n)
{
System.out.println("Matrix entered should be a Square Matrix");
System.out.println("Try Again..");
return;
}
else
{
// looping over the whole matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i < j)
{
arr[i][j] = 0;
}
}
}
System.out.println( "Lower Triangular Matrix is : ");
// printing the lower triangular matrix
printMatrix(arr);
}
}
public static void main(String[] args)
{
int arr[][] = { { 8, 7, 6 }, { 4, 2, 5 }, { 7, 9, 8 } };
//Print Original Matrix
System.out.println( "Original Matrix is : ");
printMatrix(arr);
// calling to display the lower triangular matrix
lowerTriangularMatrix(arr);
}
}
Original Matrix is :
8 7 6
4 2 5
7 9 8
Lower Triangular Matrix is :
8 0 0
4 2 0
7 9 8