Upper Triangular Matrix Program in Java and Python
Upper 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 an Upper Triangular Matrix or not.
Upper Triangular Matrix : An Upper Triangular matrix is a square matrix in which all the entries below the main diagonal (↘) are zero. The entries above or on the main diagonal themselves may or may not be zero.
Example:
\[\begin{bmatrix} 5 & 3 & 0 & 7 \\ 0 & 1 & 9 & 8 \\ 0 & 0 & 4 & 6 \\ 0 & 0 & 0 & 2 \end{bmatrix}\]
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 below the main diagonal.
Step 7: If any element below the main diagonal is non-zero, set the flag to 1.
Step 8: If the flag remains 0, display that the matrix is upper triangular; otherwise, display that it is not upper triangular.
Step 9: For each row i, check column positions before i because those positions are below the diagonal.
Step 10: Set the flag if any checked lower-side element is non-zero.
Step 11: Stop.
Explanation:
An upper triangular matrix allows values on and above the main diagonal, but every element below the main diagonal must be zero.
The program checks only those positions where the row index is greater than the column index. These positions are below the main diagonal.
A flag records whether a non-zero value is found in the lower part of the matrix. Once such a value exists, the matrix cannot be upper triangular.
The diagonal and upper part are not tested because they may contain any values. The result depends only on whether the lower part contains all zeroes.
The nested loop checks only rows below the diagonal by using column positions before the diagonal. These are the only positions that must be zero for an upper triangular matrix.
An upper triangular matrix has all elements below the main diagonal equal to zero. The main diagonal is identified by positions where row and column indexes are equal. Below this diagonal, the row index is greater than the column index. The program therefore checks only positions where i > j. If any such element is non-zero, the matrix fails the condition. This selective checking is better than scanning the whole matrix blindly, because elements on and above the diagonal are allowed to have any value.
Java Program:
/**
* The class UpperTriangularMatrix inputs a Matrix and checks whether it is an Upper Triangular Matrix or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.*;
class UpperTriangularMatrix
{
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=0;j<i;j++)
{
/* Checking that the matrix is Upper Triangular or not */
if(A[i][j]!=0) // All elements below the diagonal must be zero
{
p=1;
break;
}
}
}
if(p==0)
System.out.println("The matrix is Upper Triangular");
else
System.out.println("The matrix is not Upper Triangular");
}
}Equivalent Python Program:
# 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(0, i):
if A[i][j] != 0:
flag = 1
break
if flag == 0:
print("The matrix is Upper Triangular")
else:
print("The matrix is not Upper Triangular")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.