Program to check if matrix is lower triangular
Last Updated :
20 Feb, 2023
Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero.

Examples:
Input : mat[4][4] = {{1, 0, 0, 0},
{1, 4, 0, 0},
{4, 6, 2, 0},
{0, 4, 7, 6}};
Output : Matrix is in lower triangular form.
Input : mat[4][4] = {{1, 0, 0, 0},
{4, 3, 0, 1},
{7, 9, 2, 0},
{8, 5, 3, 6}};
Output : Matrix is not in lower triangular form.
Implementation:
C++
// Program to check lower
// triangular matrix.
#include <bits/stdc++.h>
#define N 4
using namespace std;
// Function to check matrix is in
// lower triangular form or not.
bool isLowerTriangularMatrix(int mat[N][N])
{
for (int i = 0; i < N-1; i++)
for (int j = i + 1; j < N; j++)
if (mat[i][j] != 0)
return false;
return true;
}
// Driver function.
int main()
{
int mat[N][N] = { { 1, 0, 0, 0 },
{ 1, 4, 0, 0 },
{ 4, 6, 2, 0 },
{ 0, 4, 7, 6 } };
// Function call
if (isLowerTriangularMatrix(mat))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java Program to check for
// a lower triangular matrix.
import java.io.*;
class Lower_triangular
{
int N = 4;
// Function to check matrix is
// in lower triangular form or not.
boolean isLowerTriangularMatrix(int mat[][])
{
for (int i = 0; i < N-1; i++)
for (int j = i + 1; j < N; j++)
if (mat[i][j] != 0)
return false;
return true;
}
// Driver function.
public static void main(String args[])
{
Lower_triangular ob = new Lower_triangular();
int mat[][] = { { 1, 0, 0, 0 },
{ 1, 4, 0, 0 },
{ 4, 6, 2, 0 },
{ 0, 4, 7, 6 } };
// Function call
if (ob.isLowerTriangularMatrix(mat))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Anshika Goyal.
Python3
# Python3 Program to check
# lower triangular matrix.
# Function to check matrix
# is in lower triangular
def islowertriangular(M):
for i in range(0, len(M)):
for j in range(i + 1, len(M)):
if(M[i][j] != 0):
return False
return True
# Driver function.
M = [[1,0,0,0],
[1,4,0,0],
[4,6,2,0],
[0,4,7,6]]
if islowertriangular(M):
print ("Yes")
else:
print ("No")
# This code is contributed by Anurag Rawat
C#
// C# program to check for
// a lower triangular matrix.
using System;
class Lower_triangular
{
int N = 4;
// Function to check matrix is
// in lower triangular form or not.
bool isLowerTriangularMatrix(int[, ] mat)
{
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
if (mat[i, j] != 0)
return false;
return true;
}
// Driver function.
public static void Main()
{
Lower_triangular ob = new Lower_triangular();
int[, ] mat = { { 1, 0, 0, 0 },
{ 1, 4, 0, 0 },
{ 4, 6, 2, 0 },
{ 0, 4, 7, 6 } };
// Function call
if (ob.isLowerTriangularMatrix(mat))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program to check lower
// triangular matrix.
$N = 4;
// Function to check matrix is in
// lower triangular form or not.
function isLowerTriangularMatrix($mat)
{
global $N;
for ($i = 0; $i < $N; $i++)
for ($j = $i + 1; $j < $N; $j++)
if ($mat[$i][$j] != 0)
return false;
return true;
}
// Driver Code
$mat = array(array( 1, 0, 0, 0 ),
array( 1, 4, 0, 0 ),
array( 4, 6, 2, 0 ),
array( 0, 4, 7, 6 ));
// Function call
if (isLowerTriangularMatrix($mat))
echo("Yes");
else
echo("No");
// This code is contributed by Ajit.
?>
JavaScript
<script>
// Java script Program to check for
// a lower triangular matrix.
let N = 4;
// Function to check matrix is
// in lower triangular form or not.
function isLowerTriangularMatrix(mat)
{
for (let i = 0; i < N-1; i++)
for (let j = i + 1; j < N; j++)
if (mat[i][j] != 0)
return false;
return true;
}
// Driver function.
let mat = [[ 1, 0, 0, 0 ],
[ 1, 4, 0, 0 ],
[ 4, 6, 2, 0 ],
[ 0, 4, 7, 6 ]];
// Function call
if (isLowerTriangularMatrix(mat))
document.write("Yes");
else
document.write("No");
// contributed by sravan kumar
</script>
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.
Similar Reads
Program to check if matrix is upper triangular Given a square matrix mat[][], the task is to determine whether it is in upper triangular form. A matrix is considered upper triangular if all elements below the main diagonal are zero, while the diagonal and elements above it can be any value.Examples: Input: mat[][] = [[1, 2, 3] [0, 5, 6] [0, 0, 9
4 min read
Program to check if matrix is singular or not A matrix is said to be singular if the determinant of the matrix is 0 otherwise it is non-singular .Examples: Input : 0 0 0 4 5 6 1 2 3Output : YesDeterminant value of the matrix is0 (Note first row is 0)Input : 1 0 0 4 5 6 1 2 3Output : NoDeterminant value of the matrix is 3(which is non-zero).Firs
8 min read
Program to check if N is a Centered Triangular Number Given an integer N, the task is to check if it is a Centered triangular number or not. Centered triangular number is a centered polygonal number that represents a triangle with a dot in the centre and all other dots surrounding the centre in successive triangular layers . The first few Centered tria
4 min read
Program to check Involutory Matrix Given a matrix and the task is to check matrix is involutory matrix or not. Involutory Matrix: A matrix is said to be involutory matrix if matrix multiply by itself return the identity matrix. Involutory matrix is the matrix that is its own inverse. The matrix A is said to be involutory matrix if A
7 min read
Program to check if a matrix is symmetric A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row. Examples: Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : No A Simple solution is to do fol
8 min read
Program to check idempotent matrix Given a square matrix mat[][] of order n*n, the task is to check if it is an Idempotent Matrix or not.Idempotent matrix: A matrix is said to be an idempotent matrix if the matrix multiplied by itself returns the same matrix, i.e. the matrix mat[][] is said to be an idempotent matrix if and only if M
5 min read
Program to check Identity Matrix Given a square matrix mat[][] of order n*n, the task is to check if it is an Identity Matrix.Identity Matrix: A square matrix is said to be an identity matrix if the elements of main diagonal are one and all other elements are zero. The identity Matrix is also known as the Unit Matrix. Examples: Inp
5 min read
Print lower triangular matrix pattern from given array Given an array, print the array in 2D form where upper triangle has values 0 and lower triangle has values increasing prefix sizes (First row has prefix of size 1, second row has prefix of size 2, ..)Examples : Input : 1 2 3 4 5 Output : 1 0 0 0 0 1 2 0 0 0 1 2 3 0 0 1 2 3 4 0 1 2 3 4 5 Input : 1 2
6 min read
Convert given lower triangular Matrix to 1D array Given a lower triangular matrix M[][] of dimension N * N, the task is to convert it into a one-dimensional array by storing only non-zero elements. Examples: Input: M[][] = {{1, 0, 0, 0}, {2, 3, 0, 0}, {4, 5, 6, 0}, {7, 8, 9, 10}} Output:Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}Column-wise: {1, 2, 4
13 min read
Javascript Program to check if a matrix is symmetric A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row.Examples: Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : NoA Simple solution is to do follo
2 min read