A matrix is said to be an upper triangular matrix if the elements under the principal diagonal is 0. The matrix should be a square matrix.
For example:
This is an example of an upper triangular matrix as all the elements below the principal diagonal is 0.
For any matrix A, if all elements Aij = 0 for all i>j.
INPUT: A matrix
OUTPUT: Whether it is an upper triangular matrix or nor
PROCESS:
Step 1: [taking the input]
Read n [order of the square matrix]
For i=0 to n-1 repeat
For j=0 to n-1 repeat
Read a[i][j]
[End of ‘for’ loop]
[End of ‘for’ loop]
Step 2: [Checking and printing]
for i = 1 to n-1 repeat
for j = 0 to i-1 repeat
if a[i][j] ≠ 0 then
Set f <- 1
[End of ‘if’]
[End of ‘for’ loop]
[End of ‘for’ loop]
if f=0 then
print "Upper triangular matrix"
else
print "Not an Upper Triangular Matrix"
[End of ‘if’]
Step 3: Stop.
TIME COMPLEXITY:
for (i = 1; i < n; i++)------------- O(n)
for (j = 0; j < i; j++)---- O(i)
{
if (a[i][j] != 0)
f = 1;
}
if (f == 0)---------------------------------- O(c1)
printf("\nUpper triangular matrix");
else---------------------------------------- O(c2)
printf("\nNot an Upper Triangular Matrix");
Here c1 and c2 are constant. So, the time complexity is O().
Related
Contributed by