Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Check Whether a Given Matrix is Lower Triangular Matrix...

Java Program to Check Whether a Given Matrix is Lower Triangular Matrix or Not

In the lower triangular matrix, the elements that are present above the diagonal should be 0. Given a matrix and the task is to check the matrix is in lower triangular form or not. A matrix must be a square matrix. A square matrix is called lower triangular if all the entries above the main diagonal are zero.

Example – 

Input:
    1    0    0
    1    2    0
    1    2    3
    
Output: Matrix is lower triangular matrix

Input:
    1    2    2
    2    1    1
    2    2    2
    
Output: Matrix is not a lower triangular matrix

Approach:

  1. Check if the given matrix is square or not.
  2. If yes, then traverse the upper triangle part matrix.
  3. Check if all the entries above the main diagonal are zero.
  4. If yes, then print Matrix is a lower triangular matrix.

Below is the implementation of the above approach:

Java




// Java Program to check for
// a lower triangular matrix.
 
import java.util.Scanner;
 
public class LowerTriangularMatrix {
    public static boolean
    isLowerTriangularMatrix(int[][] matrix)
    {
        for (int i = 0; i < matrix[0].length; i++) {
            for (int j = i + 1; j < matrix[0].length; j++) {
                if (matrix[i][j] != 0)
                    return false;
            }
        }
        return true;
    }
    public static void main(String args[])
    {
        int rows = 3, columns = 3;
 
        // returns true if number of rows and columns are
        // equal else false
        if (rows == columns) {
            // initializing the matrix
            int Matrix[][]
                = { { 1, 0, 0 }, { 2, 3, 0 }, { 5, 6, 7 } };
 
            // Display the values of matrix
            System.out.println("Matrix is : ");
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    System.out.print(Matrix[i][j] + "\t");
                }
                System.out.println();
            }
 
            // Function call
            boolean result
                = isLowerTriangularMatrix(Matrix);
 
            // returns true if matrix is lower triangular
            // matrix else false
            if (result == true) {
                System.out.println(
                    "Matrix is lower triangular matrix");
            }
            else {
                System.out.println(
                    "Matrix is not lower triangular matrix");
            }
        }
        else {
            System.out.println(
                "Number of rows and number of columns should be equal");
        }
    }
}


Output

Matrix is : 
1    0    0    
2    3    0    
5    6    7    
Matrix is lower triangular matrix

Time Complexity: O(N2), where N is the length of the row in a square matrix.

Auxiliary Space: O(1), no extra space is required, so it is a constant.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments