Latest :

C Program Lower Triangular Matrix or Not | C Programs

Write a C program to determine whether the given matrix is a lower triangular matrix or not. We call a square matrix a lower triangular matrix when all the elements above its main diagonal are zero whereas the ones below and on the diagonal are non-zero. For this, we require the size of the square matrix and the elements of the matrix (2D array) as inputs.

Our desired resultant output is a statement telling whether the given matrix is a lower triangular matrix or not.

The first step always is to gather all the necessary inputs. So, we’ll read our inputs at runtime by using the predefined input function, scanf(). This function reads any primitive datatype input at runtime and based on the format specifier, the type of input being read is determined. For integers we generally use ‘%d’ as format specifier.

So, we first read the size of square matrix (n) and then, create a square matrix of the same size (arr[n][n]) using dynamic memory allocation (calloc). We then read the elements or data values of the square matrix (2D array) by iterating through the loop.

int n,i, j,**arr1,c=0;

printf(“\nEnter the N value for square matrix :”);

scanf(“%d”,&n);

arr1 = (int **)calloc(n,sizeof(int*));

for (i=0; i<n; i++)

arr1[i] =(int *)calloc(n,sizeof(int*));

printf(“\nEnter the elements in %dX%d matrix :”,n,n);

for (i = 0; i<n;i++) {

for(j=0;j<n; j++) {

scanf(“%d”,&arr1[i][j]);

}

}

Later, we display the input array in matrix format with every row being displayed in a new line on the console screen. This is done using predefined output function, printf() using the same format specifier as that of the input.

Then, we iterate through the array and for every ith row we check whether from (i+1)th column all the elements are zero or not.

If some element is non-zero for the given condition then, we change the flag variable (c) which is initially zero to one and break the loop and exit out of the iteration completely.

for (i=0;i<n;i++) {

for(j=i+1;j<n;j++) {

if(arr1[i][j]) {

c=1;

break;

}

}

if(c)

break;

}

After completion of all the iterations, if the value of c is 1 then, the given matrix is not a lower triangular matrix because, not all elements above the main diagonal are zero. Hence, we display the same on the output console screen using printf(). Else, if it is zero then, the given matrix is a lower triangular matrix as no element above main diagonal is non-zero. We display the same on output console screen.

if(c)

printf(“given matrix is not Lower triangular matrix”);

else

printf(“given matrix is Lower triangular matrix”);

Output:

C Program Lower Triangular Matrix Using Function

Using functions has always been advantageous especially because it makes the code reusable and also enhances the readability of the code. If we make user of functions then, if the same logic is necessary somewhere else in the code then, we can just make a function call thereby, making that part of the code reusable.

Also, writing it within separate function block makes the code cleaner.

To implement this, the main method first reads the size of square matrix and creates a 2D array of the same size dynamically using calloc(). This then invokes the input() function. This is a user-defined function that takes the array and its size as parameters and reads the elements of the matrix. Later, for better understanding of the user, we call the user-defined function print() which displays the 2D array in matrix format with every row in a new line.

Then, we call the check() function and pass array and size as parameters. This user-defined function consists of the same logic as discussed above to determine whether the given matrix is a lower triangular matrix or not and displays the same on the output console screen using printf().

x

Check Also

C Program To Find Reverse Of An Array – C Programs

C program to find the reverse of an array – In this article, we will ...