C Program to Display Lower Triangular Matrix

«
»

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 ith row and jth column and M[m][n] represent Matrix with total m rows and n columns.

Problem Description

Write a C program to display the lower triangular matrix.

Problem Solution

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.

advertisement
advertisement
Method 1: Display Lower Triangular Matrix in C

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 –

1st Row 11 1 12 0 13 0
2nd Row 21 4 22 5 23 0
3rd Row 31 7 32 8 33 9
1st Column 1nd Column 3rd 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)}

Program/Source Code

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

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.

advertisement

Time Complexity: O(n2)
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(n2).

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(n2)
The Space Complexity will be O(m×n) and for square matrix of order n it will be O(n2) as Matrix M is declared and will be using auxiliary space to store the elements.

Runtime Test Cases

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

advertisement
Method 2: C Program to Check Matrix is a Lower Triangular Matrix or Not

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

Program/Source Code

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");
}
Program Explanation

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.

Runtime Test Cases

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”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.