Friday, July 11, 2025
HomeData Modelling & AIC++ Program to check if matrix is lower triangular

C++ Program to check if matrix is lower triangular

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.

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; 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;
}


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.

Please refer complete article on Program to check if matrix is lower triangular for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32132 POSTS0 COMMENTS
Milvus
66 POSTS0 COMMENTS
Nango Kala
6516 POSTS0 COMMENTS
Nicole Veronica
11663 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11720 POSTS0 COMMENTS
Shaida Kate Naidoo
6608 POSTS0 COMMENTS
Ted Musemwa
6872 POSTS0 COMMENTS
Thapelo Manthata
6570 POSTS0 COMMENTS
Umr Jansen
6560 POSTS0 COMMENTS