
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to check if matrix is lower triangular in C++
Given a square matrix M[r][c] where ‘r’ is some number of rows and ‘c’ are columns such that r = c, we have to check that ‘M’ is lower triangular matrix or not.
Lower Triangular Matrix −
Lower triangular matrix is a matrix in which the elements below the main diagonal(including the main diagonal) are not zero and above elements are zero only.
Like in the given Example below −
In above figure the red highlighted elements are upper elements from the main diagonal which are zero and rest elements are non-zero.
Example
Input: m[3][3] = { {1, 0, 0}, {2, 3, 0}, {4, 5, 6}} Output: yes Input: m[3][3] == { {3, 0, 1}, {6, 2, 0}, {7, 5, 3} } Output: no
Algorithm
Start Step 1 -> define macro as #define size 4 Step 2 -> declare function to check matrix is lower triangular matrix bool check(int arr[size][size]) Loop For int i = 0 and i < size and i++ Loop For int j = i + 1 and j < size and j++ If (arr[i][j] != 0) return false End End End return true step 3 -> In main() Declare array int arr[size][size] = { { 1, 0, 0, 0 }, { 2, 3, 0, 0 }, { 4, 5, 6, 0 }, { 7, 8, 9, 10 } } If (check(arr)) Print its a lower triangular matrix Else Print its not a lower triangular matrix Stop
Example
#include <bits/stdc++.h> #define size 4 using namespace std; // check matrix is lower triangular matrix bool check(int arr[size][size]){ for (int i = 0; i < size; i++) for (int j = i + 1; j < size; j++) if (arr[i][j] != 0) return false; return true; } int main(){ int arr[size][size] = { { 1, 0, 0, 0 }, { 2, 3, 0, 0 }, { 4, 5, 6, 0 }, { 7, 8, 9, 10 } }; if (check(arr)) cout << "its a lower triangular matrix"; else cout << "its not a lower triangular matrix"; return 0; }
Output
its a lower triangular matrix
- Related Articles
- JavaScript Program to check if the matrix is lower Triangular
- Check if the matrix is lower triangular using Python
- Program to check if matrix is upper triangular in C++
- C# Program to Illustrate Lower Triangular Matrix
- JavaScript Program to Check if Matrix is Upper Triangular
- Swift Program to Display Lower Triangular Matrix
- Program to print Lower triangular and Upper triangular matrix of an array in C
- Print lower triangular matrix pattern from given array in C Program.
- How to Print Lower Triangular Matrix in Java?
- Find Product Between Lower Triangular & Upper Triangular Matrix in Java
- How to replace upper triangular matrix with lower triangular matrix and vice versa in R?
- Program to check if a matrix is symmetric in C++
- C# program to Illustrate Upper Triangular Matrix
- C++ Program to Check if a Matrix is Invertible
- Program to check if a matrix is Binary matrix or not in C++

Advertisements