Java Program to Display Lower Triangular Matrix

In the previous article, we have discussed Java Program to Check Whether Two Matrices are Equal or Not

In this article we are going to see how we can write a program to display the lower triangular matrix in JAVA language.

Java Program to Display Lower Triangular Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Lower triangular matrix is a square matrix in which Aij elements are 0 i.e Aij=0 for j>i means all elements above the main diagonal are zero.

                                           1  0  0
Lower Triangular Matrix=   7  4  0
                                           8  9  3

Let’s see different ways to Display Lower Triangular Matrix.

Method-1: Java Program to Display Lower Triangular Matrix By Static Initialization of Array Elements

Approach:

  • Declare two arrays of size 3×3, one with elements.
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops for all col>row locations store a zero, else copy the elements from the main array to the resultant array.
  • Print the resultant array.

Program:

public class matrix{
    public static void main(String args[])
    {

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{9,2,2},{4,4,2},{7,2,6}}, res[][] = new int[3][3];
        int row, col ;
        
        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        // Loops to store the lower triangular matrix 
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                if(row<col)
                    res[row][col]=0;
                else
                    res[row][col]= arr[row][col];
            }   
        System.out.print("\n\nThe lower triangular matrix : ");
        printMatrix(res);
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
}


Output:

The matrix elements are : 
9 2 2 
4 4 2 
7 2 6

The lower triangular matrix : 
9 0 0 
4 4 0 
7 2 6

Method-2: Java Program to Display Lower Triangular Matrix By Dynamic Initialization of Array Elements

Approach:

  • Initialize two arrays of size 3×3.
  • Ask the user for input of array elements and store them in the one array using two for loops.
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops for all col>row locations store a zero, else copy the elements from the main array to the resultant array.
  • Print the resultant array.

Program:

import java.util.Scanner;
public class matrix{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3], res[][] = new int[3][3];
        int row, col ;
        
        // Taking matrix1 input
        System.out.println("\nEnter matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();
        
        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        // Loops to store the lower triangular matrix
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                if(row<col)
                    res[row][col]=0;
                else
                    res[row][col]= arr[row][col];
            }   
        System.out.print("\n\nThe lower triangular matrix : ");
        printMatrix(res);
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
}

Output:

Enter matrix elements : 1 2 3 1 2 1 3 4 5 6
The matrix elements are : 
1 2 3 
1 2 1 
3 4 5

The lower triangular matrix : 
1 0 0 
1 2 0 
3 4 5

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: