What is Upper Triangular Matrix? An upper triangular matrix is a square matrix in which all theelements below the diagonal are zero. We consider a Matrix U with ‘i’ rows and ‘j’ columns, thenmathematically we represent the upper triangular matrix as

In another word strictly upper triangular matrix is an upper triangular matrix having 0’s along the diagonal as well, i.e., U(ij)=0 for i>=j.


In another word strictly upper triangular matrix is an upper triangular matrix having 0’s along the diagonal as well, i.e., U(ij)=0 for i>=j.

import java.util.*;
class UpperTriangularMatrix
{
public static void main(String args[])throws Exception
{
Scanner sy=new Scanner(System.in);
System.out.print("Enter the order of the matrix : "); //get order of matrix
int n=sy.nextInt();
int Arr[][]=new int[n][n];
for(int i=;i<n;i++) // get Input in the matrix
{
for(int j=;j<n;j++)
{
System.out.print("Enter an element : ");
Arr[i][j]=sy.nextInt();
}
}
System.out.println("____________________"); // Print the matrix
System.out.println("The Matrix you enter is : ");
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
System.out.print(Arr[i][j]+"t");
}
System.out.println();
}
System.out.println("_____________________");
int k=;
for(int i=;i<n;i++) // Check the matrix is Upper Triangular Matrix or not
{
for(int j=;j<i;j++)
{
if(Arr[i][j]!=) // All elements below the diagonal must be zero
{
p=1;
break;
}
}
}
if(p==)
System.out.println("The matrix is an Upper Triangular Matrix");
else
System.out.println("The matrix is not an Upper Triangular Matrix");
}
}
Output:
Enter the order of the matrix : 4
Enter an element : 1 Enter an element : 2 Enter an element : 3 Enter an element : 4 Enter an element : 0 Enter an element : 6 Enter an element : 7 Enter an element : 8 Enter an element : 0 Enter an element : 0 Enter an element : 2 Enter an element : 3 Enter an element : 0 Enter an element : 0 Enter an element : 0 Enter an element : 7 _____________________ The Matrix is : 1 2 3 4 0 6 7 8 0 0 2 3 0 0 0 7 _____________________ The matrix is an Upper Triangular Matrix |
No comments:
Post a Comment