Open In App

Program to check if matrix is upper triangular

Last Updated : 01 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero.

Examples: 

Input : mat[4][4] = {{1, 3, 5, 3},
{0, 4, 6, 2},
{0, 0, 2, 5},
{0, 0, 0, 6}};
Output : Matrix is in Upper Triangular form.
Input : mat[4][4] = {{5, 6, 3, 6},
{0, 4, 6, 6},
{1, 0, 8, 5},
{0, 1, 0, 6}};
Output : Matrix is not in Upper Triangular form.

Implementation:

C++




// Program to check upper triangular matrix.
#include <bits/stdc++.h>
#define N 4
using namespace std;
 
// Function to check matrix is in upper triangular
// form or not.
bool isUpperTriangularMatrix(int mat[N][N])
{
    for (int i = 1; i < N; i++)
        for (int j = 0; j < i; j++)
            if (mat[i][j] != 0)
                return false;
    return true;
}
 
// Driver function.
int main()
{
    int mat[N][N] = { { 1, 3, 5, 3 },
                      { 0, 4, 6, 2 },
                      { 0, 0, 2, 5 },
                      { 0, 0, 0, 6 } };
    if (isUpperTriangularMatrix(mat))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java




// Java Program to check upper
// triangular matrix.
import java.util.*;
import java.lang.*;
 
public class GfG
{
    private static final int N = 4;
 
    // Function to check matrix is in
    // upper triangular form or not.
    public static Boolean isUpperTriangularMatrix(int mat[][])
    {
        for (int i = 1; i < N ; i++)
            for (int j = 0; j < i; j++)
                if (mat[i][j] != 0)
                    return false;
        return true;
    }
     
    // driver function
    public static void main(String argc[]){
        int[][] mat= { { 1, 3, 5, 3 },
                       { 0, 4, 6, 2 },
                       { 0, 0, 2, 5 },
                       { 0, 0, 0, 6 } };
                     
        if (isUpperTriangularMatrix(mat))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
/* This code is contributed by Sagar Shukla */


Python3




# Python3 Program to check upper
# triangular matrix.
 
# Function to check matrix
# is in upper triangular
def isuppertriangular(M):
    for i in range(1, len(M)):
        for j in range(0, i):
            if(M[i][j] != 0):
                    return False
    return True
     
# Driver function.
M = [[1,3,5,3],
    [0,4,6,2],
    [0,0,2,5],
    [0,0,0,6]]
 
if isuppertriangular(M):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by Anurag Rawat


C#




// C# Program to check upper
// triangular matrix.
using System;
 
public class GfG
{
    private static int N = 4;
 
    // Function to check matrix is in
    // upper triangular form or not.
    public static bool isUpperTriangularMatrix(int [,]mat)
    {
        for (int i = 1; i < N ; i++)
            for (int j = 0; j < i; j++)
                if (mat[i, j] != 0)
                    return false;
        return true;
    }
     
    // Driver function
    public static void Main(){
        int [,]mat= { { 1, 3, 5, 3 },
                    { 0, 4, 6, 2 },
                    { 0, 0, 2, 5 },
                    { 0, 0, 0, 6 } };
                     
        if (isUpperTriangularMatrix(mat))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
/* This code is contributed by vt_m */


Javascript




<script>
// Java script  Program to check upper
// triangular matrix.
let  N = 4;
 
    // Function to check matrix is in
    // upper triangular form or not.
    function isUpperTriangularMatrix(mat)
    {
        for (let i = 1; i < N ; i++)
            for (let j = 0; j < i; j++)
                if (mat[i][j] != 0)
                    return false;
        return true;
    }
     
    // driver function
     
        let mat= [[1, 3, 5, 3 ],
                    [ 0, 4, 6, 2 ],
                    [ 0, 0, 2, 5 ],
                    [ 0, 0, 0, 6 ]];
                     
        if (isUpperTriangularMatrix(mat))
            document.write("Yes");
        else
            document.write("No");
     
         
// contributed by sravan kumar
</script>


PHP




<?php
// PHP Program to check upper
// triangular matrix.
$N = 4;
 
// Function to check matrix is
// in upper triangular form or
// not.
function isUpperTriangularMatrix($mat)
{
    global $N;
    for ($i = 1; $i < $N; $i++)
        for ($j = 0; $j < $i; $j++)
            if ($mat[$i][$j] != 0)
                return false;
    return true;
}
 
    // Driver Code
    $mat = array(array(1, 3, 5, 3),
                 array(0, 4, 6, 2) ,
                 array(0, 0, 2, 5),
                 array(0, 0, 0, 6));
                 
    if (isUpperTriangularMatrix($mat))
        echo "Yes";
    else
        echo"No";
 
// This code is contributed by anuj_67.
?>


Output

Yes







Time Complexity: O(n2), where n represents the number of rows and columns of the matrix.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Approach 2:

The approach used in the program is to check whether all the elements of the matrix below the diagonal are zero or not. If all the elements below the diagonal are zero, then the matrix is considered to be an upper triangular matrix. This is achieved by looping through all the elements of the matrix below the diagonal and checking if each element is zero or not. If any non-zero element is found, the matrix is not upper triangular. If all the elements below the diagonal are zero, the matrix is upper triangular.

C++




#include <bits/stdc++.h>
#define N 4
using namespace std;
 
// Function to check matrix is in upper triangular
// form or not.
bool isUpperTriangularMatrix(int mat[N][N])
{
    for (int i = 1; i < N; i++)
        for (int j = 0; j < i; j++)
            if (mat[i][j] != 0)
                return false;
    return true;
}
 
// Driver function.
int main()
{
    int mat[N][N] = { { 1, 3, 5, 3 },
                      { 0, 4, 6, 2 },
                      { 0, 0, 2, 5 },
                      { 0, 0, 0, 6 } };
    if (isUpperTriangularMatrix(mat))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java




public class GFG {
 
    // Function to check if a matrix is in upper triangular form or not
    public static boolean isUpperTriangularMatrix(int[][] mat) {
        int n = mat.length; // Size of the matrix
 
        // Iterate over each row starting from the second row
        for (int i = 1; i < n; i++) {
            // Iterate over each column up to the diagonal of the current row
            for (int j = 0; j < i; j++) {
                // If any element below the diagonal is non-zero, it's not an
              // upper triangular matrix
                if (mat[i][j] != 0) {
                    return false;
                }
            }
        }
 
        // If all elements below the diagonal are zero,
      // it's an upper triangular matrix
        return true;
    }
 
    public static void main(String[] args) {
        int[][] mat = { { 1, 3, 5, 3 },
                        { 0, 4, 6, 2 },
                        { 0, 0, 2, 5 },
                        { 0, 0, 0, 6 } };
 
        if (isUpperTriangularMatrix(mat)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}


Python




def is_upper_triangular_matrix(mat):
    n = len(mat)
    for i in range(1, n):
        for j in range(i):
            if mat[i][j] != 0:
                return False
    return True
 
# Driver function.
if __name__ == "__main__":
    mat = [[1, 3, 5, 3],
           [0, 4, 6, 2],
           [0, 0, 2, 5],
           [0, 0, 0, 6]]
     
    if is_upper_triangular_matrix(mat):
        print("Yes")
    else:
        print("No")


C#




using System;
 
class GFG
{
    const int N = 4;
 
    // Function to check if the matrix is in upper triangular form or not.
    static bool IsUpperTriangularMatrix(int[,] mat)
    {
        for (int i = 1; i < N; i++)
        {
            for (int j = 0; j < i; j++)
            {
                // If any element below the main diagonal is non-zero, the matrix is not upper triangular.
                if (mat[i, j] != 0)
                    return false;
            }
        }
        return true;
    }
 
    // Driver function.
    static void Main()
    {
        int[,] mat = { { 1, 3, 5, 3 },
                       { 0, 4, 6, 2 },
                       { 0, 0, 2, 5 },
                       { 0, 0, 0, 6 } };
 
        if (IsUpperTriangularMatrix(mat))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}


Javascript




function isUpperTriangularMatrix(mat) {
    let n = mat.length; // Size of the matrix
 
    // Iterate over each row starting from the second row
    for (let i = 1; i < n; i++) {
        // Iterate over each column up to the diagonal of the current row
        for (let j = 0; j < i; j++) {
            // If any element below the diagonal is non-zero, it's not an
            // upper triangular matrix
            if (mat[i][j] !== 0) {
                return false;
            }
        }
    }
 
    // If all elements below the diagonal are zero,
    // it's an upper triangular matrix
    return true;
}
 
let mat = [
    [1, 3, 5, 3],
    [0, 4, 6, 2],
    [0, 0, 2, 5],
    [0, 0, 0, 6]
];
 
if (isUpperTriangularMatrix(mat)) {
    console.log("Yes");
} else {
    console.log("No");
}


Output

Yes







Time Complexity: O(n^2)

Auxiliary Space: O(n^2)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads