A Matrix is said to be a **Lower Triangular matrix** if all the Elements above the diagonal Elements are zero(0) or in other words All the elements including diagonal elements and below them are non- zero(≠0)

Mathematically, if a Matrix **M[m][n]** is a Lower Trangular Matrix then it satisfies the condition:

A[i][j] = 0 for i<j

A[i][j] ≠ 0 for i>=j

Here A[i][j] represent the element present in i^{th} row and j^{th} column and **M[m][n]** represent Matrix with total m rows and n columns.

Write a C program to display the lower triangular matrix.

1. Take input from the user.

2. Perform matrix operations.

3. Print lower triangular matrix.

There are two ways to display lower triangular matrix in C language. Let’s take a detailed look at all the approaches.

In this method, we take a input matrix from the user and prints the lower triangular matrix.

**Example:**

Let’s say M is a 3×3 Matrix with entries as follows –

1^{st} Row |
_{11} 1 |
_{12} 0 |
_{13} 0 |

2^{nd} Row |
_{21} 4 |
_{22} 5 |
_{23} 0 |

3^{rd} Row |
_{31} 7 |
_{32} 8 |
_{33} 9 |

1^{st} Column |
1^{nd} Column |
3^{rd} Column |

Now we, conclude that the given matrix **M** is Lower Triangular Matrix as all elements where i<j = 0 and for all elements, where i>=j are ≠ 0.

As seen element at (i,j) = 0 for {(1,2),(1,3),(2,3)} and elements at(i,j) ≠ 0 for {(1,1),(2,2),(3,3),(2,1),(3,1),(3,2)}

Here is source code of the C Program to display lower triangular matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C Program to Display Lower Triangular Matrix */ #include <stdio.h> int main() { int i,j; int Max_Row; int Max_Column; printf("Enter the Number of Rows: "); scanf("%d", &Max_Row); printf("Enter the Number of Columns: "); scanf("%d", &Max_Column); int M[Max_Row][Max_Column]; printf("Enter the Elements in the Matrix: \n"); for (i = 0; i < Max_Row; i++) { for (j = 0; j < Max_Column; j++) { scanf("%d",&M[i][j]); } } // Lower Triangular Matrix Logic for (i = 0; i < Max_Row; i++) { for (j = 0; j < Max_Column; j++) { if (i < j) { M[i][j] = 0; } } } printf("The Lower Traingular Matrix is: \n"); for (i = 0; i < Max_Row; i++) { for (j = 0; j < Max_Column; j++) { printf("%d ", M[i][j]); } printf("\n"); } return 0; }

1. Declare **Max_row** and **Max_Column** variable and took the user input and then declare a matrix M of size **Max_Row×Max_Column** and using nested **for loop** and the **scanf()** funtion took the input for each position in the matrix.

2. Now, Using the lower triangular matrix defintion

if (i < j) { M[i][j] = 0; }

3. Decalre the above condition within the nested for loop as shown in the code, this condition re-initalize the value of **M[i][j] = 0**, where i is less than than j that is, the values above the diagonal elements thus making the matrix a lower traingular matrix.

4. At last printe the Lower Traingular Matrix using the nested for loop and using **printf()** funtion.

**Time Complexity: O(n ^{2})**

The Time Complexity of the above program is O(m×n), where m is number of rows and n is number of colums or let’s say for a square matrix of order n the time complexity will be O(n

^{2}).

The time complexity is due to the nested for loop where the first is iterating over rows and second is iterating over the columns.

**Space Complexity: O(n ^{2})**

The Space Complexity will be O(m×n) and for square matrix of order n it will be O(n

^{2}) as Matrix M is declared and will be using auxiliary space to store the elements.

In this case, we enter “3” for the number of rows and “3” for the number of columns, and the matrix elements to determine the lower triangular matrix are “4, 3, 2, 5, 3, 2, 1, 2 and 3.”

Enter the Number of Rows: 3 Enter the Number of Columns: 3 Enter the Elements in the Matrix: 4 3 2 5 3 2 1 2 3 The Lower Traingular Matrix is: 4 0 0 5 3 0 1 2 3

In this method, we take an input matrix from the user and check if it is a lower triangle matrix.

Here is source code of the C Program to check if it is a lower triangle matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C Program to check if it is a lower triangle matrix */ #include <stdio.h> void main() { int array[3][3], i, j, flag = 0 ; printf("\n\t Enter the value of Matrix : "); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &array[i][j]); } } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (array[i] < array[j] && array[i][j] == 0) { flag = flag + 1; } } } if (flag == 3) printf("\n\n Matrix is a Lower triangular matrix"); else printf("\n\n Matrix is not a lower triangular matrix"); }

1. In this C program, using for loop we are reading the coefficient value of the matrix to the variable **array[i][j]**.

2. In for loop the variable ‘**i**’ is initialized to zero and checks the value of ‘**i**’ variable is less than three.

3. In another for loop the value of ‘**j**’ variable is initialized to zero and checks the value of ‘**j**’ variable is less than three.

4. Using if condition statement inside the for loop, we are checking the condition that the value of **array[i]** variable is less than the value of **array[j]** variable, and also the value of **array[i][j]** variable is equal to zero using logical AND operator.

5. If the condition is true, it will execute the statement by incrementing the ‘**flag**’ variable value as one.

6. If the value of ‘**flag**’ variable is equal to 3, it will display the output as matrix is a lower triangular matrix.

7. Otherwise, it will display the output as not a triangular matrix.

**Test case 1:** In this case, we enter “1, 2, 0, 1, 0, 0, 0, 0, 0” as the matrix elements to check if it is a lower triangular matrix.

Enter the value of Matrix : 1 2 0 1 0 0 0 0 0 Matrix is not a lower triangular matrix

**Test case 1:** In this case, we enter “1, 0, 0, 1, 1, 0, 1, 1, 1” as the matrix elements to check if it is a lower triangular matrix.

Enter the value of Matrix : 1 0 0 1 1 0 1 1 1 Matrix is a Lower triangular matrix

**Conclusion:**

There exist a variation of Lower Triangular Matrix known as Strictly Lower Triangular Matrix in the elements including diagonal and above are zero that is, for M[i][j] = 0 for all i<=j.

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Practice Computer Science MCQs