techhipbettruvabetnorabahisbahis forumutaraftarium24edueduseduseduedusedueduseduseduedu C Program Lower Triangular Matrix or Not | C Programs
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().

techhipbettruvabetnorabahisbahis forumutaraftarium24eduedusedusedusedueduedueduseduedus
x

Check Also

C Program To Input Any Alphabet And Check Whether It Is Vowel Or Consonant

C program to input any alphabet and check whether it is vowel or consonant – ...

cratosroyalbetgrandpashabetgrandpashabethttps://mobilebarhawaii.com/ballettea tropazhacklinkgrandpashabetonwinonwin girişselcuksportshdbahsegelatakoy escortatakoy escortcratosroyalbetonwinligobetkingroyal girişJojobetgrandpashabetİzmit escortbakırköy escortdeneme bonusu veren sitelerhalkali escortgrandpashabetonwinyurtiçi kargoesenyurt anal escortbets10starzbetmaltcasinomaltcasinojojobet girişfixbetkucukcekmece escortistanbul escortstarzbetjojobetfast creditsightcareİstanbul Escortantalya escortNorabahisfixbetmaltepe escortÜmraniye Escortholiganbetbets10takipçi satın aldeneme bonusu veren sitelerkingroyalnerobetetrabetcashback girişcasibomjojobetholiganbetsahabetcasibomcasibommariobetgrandpashabetGrandpashabetGrandpashabetholiganbetdeneme bonusu veren sitelermarsbahisGrandpashabetholiganbet güncel girişjojobet girişmarsbahisdeneme bonusu veren sitelermeritkingmatbethttps://markaescort.org/bolgeler/kartalartemisbet güncel girişescort kadıköyholiganbetsahabet girişonwin girişpusulabetmarsbahis giriş güncelmarsbahis güncel girişsahabetbahiscomkulisbetarnavutkoy escorttempobettarafbetsavoybettingsahabetsekabetmobilbahismadridbetbetturkeyholiganbet girişzbahismeritbetbets10ultrabetdinamobetbetkanyonstarzbetsuperbetinbetzulasuperbetinbetebetmilanobetbetturkey girişholiganbet girişmarsbahistempobetbetebetpusulabetonwin,onwin giriş,onwin güncel giriş,onwin giriş adresisahabetsekabet girişmatbet girişmarsbahis girişonwinmarsbahis girişsahabet girişsekabet girişmatbetbetturkey,betturkey girişimajbet,imajbet giriş,imajbet güncel giriş,imajbet resmi girişsahabetsahabetholiganbetkralbetmarsbahis,marsbahis güncel giriş , marsbahis giriş,marsbahis adresonwinsahabet,sahabet giriş,sahabet güncel giriş,sahabet resmi girişpusulabetmeritkingmavibetmobilbahiskingroyalbetebetnakitbahisMatbetholiganbetmetamaskmarsbahiskingroyalonwinmatbetpusulabetholiganbet girişmarsbahisgrandpashabetdinamobetbayconti girişGrandpashabetbetebetholiganbetsekabetbetsatholiganbetjojobetkingbettinggrandpashabet girişgrandpashabetjustin tv1xbethttps://canlicasino.shorthandstories.com/https://canlicasino.shorthandstories.com/deneme bonusu veren sitelerjojobetimajbet girişjojobet girişjojobet girişgamdomsahabetİzmit escortdeneme bonusukralbet güncel girişbetexperholiganbet girişjojobetsekabetsekabet girişpadişahbetistanbul escortgrandpashabetmarsbahistipobetmadridbetHoliganbet marsbahisholiganbetholiganbetimajbetjojobetistanbul, escortdumanbetcasibomjojobetdeneme bonusudeneme bonusu veren sitelerrestbetgrandpashabet girişgrandpashabet girişmadridbetLimanbetdeneme bonusu veren sitelerdeneme bonusu veren siteleronwinonwinonwinbetturkey girişholiganbetdinamobetmarsbahis