Friday , 24 March 2017
Home » Array Related Programs » Java Program to check for Lower Triangular Matrix

Java Program to check for Lower Triangular Matrix

Question:

Write a Program in Java to input a 2-D square matrix and check whether it is a Lower Triangular Matrix or not.

Lower Triangular Matrix : A Lower Triangular matrix is a square matrix in which all the entries above the main diagonal (↘) are zero. The entries below or on the main diagonal themselves may or may not be zero.

Example:

\begin{bmatrix} 5 & 0 & 0 & 0 \\ 3 & 1 & 0 & 0 \\ 4 & 9 & 4 & 0 \\ 6 & 8 & 7 & 2 \end{bmatrix}

Solution:

/**
* The class LowerTriangularMatrix inputs a Matrix and checks whether it is a Lower Triangular Matrix or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class LowerTriangularMatrix
{
    public static void main(String args[])throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the size of the matrix : ");
        int m=sc.nextInt();
        int A[][]=new int[m][m];
        
        /* Inputting the matrix */
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                System.out.print("Enter an element : ");
                A[i][j]=sc.nextInt();
            }
        }

        /* Printing the matrix */
        System.out.println("*************************");
        System.out.println("The Matrix is : ");
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                System.out.print(A[i][j]+"\t");
            }
            System.out.println();
        }
        System.out.println("*************************");
        
        int p=0;
        
        for(int i=0;i<m;i++)
        {
            for(int j=i+1;j<m;j++)
            {
                /* Checking that the matrix is Lower Triangular or not */
                if(A[i][j]!=0) // All elements above the diagonal must be zero
                {
                    p=1;
                    break;
                }
            }
        }
        
        if(p==0)
            System.out.println("The matrix is Lower Triangular");
        else
            System.out.println("The matrix is not Lower Triangular");
    }
}

Output:

Enter the size of the matrix : 4
Enter an element : 5
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 3
Enter an element : 1
Enter an element : 0
Enter an element : 0
Enter an element : 4
Enter an element : 9
Enter an element : 4
Enter an element : 0
Enter an element : 6
Enter an element : 8
Enter an element : 7
Enter an element : 2
*************************
The Matrix is :
5 0 0 0
3 1 0 0
4 9 4 0
6 8 7 2
*************************
The matrix is Lower Triangular

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

2 comments

  1. Awsome logics ?

Leave a Reply

Your email address will not be published. Required fields are marked *