SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Lower Triangular Matrix Program in Java and Python

14 February 2015

Lower Triangular Matrix program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program 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}\]

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

Algorithm:

Step 1: Start.

Step 2: Accept the order of the square matrix.

Step 3: Input all elements of the matrix.

Step 4: Display the matrix.

Step 5: Initialize a flag to 0.

Step 6: Check only the elements above the main diagonal.

Step 7: If any element above the main diagonal is non-zero, set the flag to 1.

Step 8: If the flag remains 0, display that the matrix is lower triangular; otherwise, display that it is not lower triangular.

Step 9: For each row i, start the column loop from i + 1 because those positions are above the diagonal.

Step 10: Set the flag if any checked position contains a non-zero value.

Step 11: Stop.

Explanation:

A lower triangular matrix allows values on and below the main diagonal, but every element above the main diagonal must be zero.

The program does not need to check the whole matrix. It checks only positions where the column index is greater than the row index, because those positions lie above the main diagonal.

A flag is initialized before checking. If any element above the diagonal is non-zero, the flag changes value to show that the matrix has failed the lower triangular condition.

After all required positions are checked, the flag decides the final message. This keeps the logic efficient and avoids unnecessary checks below the diagonal.

The nested loop starts the column from i + 1. This is a deliberate index choice because columns after the diagonal in a row are exactly the elements above the main diagonal.

A lower triangular matrix has all elements above the main diagonal equal to zero. The main diagonal is identified by equal row and column indexes. Above this diagonal, the column index is greater than the row index. The program therefore checks positions where j > i. If any of these positions contains a non-zero value, the matrix is not lower triangular. Elements on the diagonal and below it are allowed to contain any value, so they should not be treated as errors.

Java Program:

Java
/**
* 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");
    }
}

Equivalent Python Program:

Python
# Read the matrix or array size and store the values for indexed processing.
# Nested loops are used because each row/column or array position must be checked.
# Print the processed array or matrix in the required output format.

m = int(input("Enter the size of the matrix: "))
A = []

for i in range(m):
    row = []
    for j in range(m):
        row.append(int(input("Enter an element: ")))
    A.append(row)

print("The Matrix is:")
for i in range(m):
    for j in range(m):
        print(A[i][j], end="	")
    print()

flag = 0
for i in range(m):
    for j in range(i + 1, m):
        if A[i][j] != 0:
            flag = 1
            break

if flag == 0:
    print("The matrix is Lower Triangular")
else:
    print("The matrix is not Lower Triangular")

Output:

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

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →