Here's a C program to display only the positive elements of an array using for loops and IF conditions.
Output of above program is
Enter the limit : 5
Enter the elements :
10 -20 30 -40 -50
The positive elements are :
10 30
# include <stdio.h> # include <conio.h> void main() { int a[20], i, n ; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nEnter the elements :\n\n") ; for(i = 0 ; i < n ; i++) scanf("%d", &a[i]) ; printf("\nThe positive elements are :\n\n") ; for(i = 0 ; i < n ; i++) { if(a[i] > 0) printf("%d\t", a[i]) ; } getch() ; }
Output of above program is
Enter the limit : 5
Enter the elements :
10 -20 30 -40 -50
The positive elements are :
10 30
Explanation of above program
The program is very easy. In it we have an array a[20] of type integer (int) and size 20. Then we're asking the user to enter a limit between 1 and 20 i.e.it's up to user how many entries he wants to make. Then with the help of a for loop we enters the all the integers up to the limit previously specified by the user.
Now that our array a[20] is populated, next step is to find all the positive values in that array. For this we have another for loop which runs from 0 to n - 1 (one less than the limit specified by the user) and using an IF condition we check whether the value inside the array at a particular index is greater than 0 i.e. positive and if the IF condition becomes true than we print that value otherwise we continue with next values until the loop is terminated.
Also look at: With little modification we can make this program to check for negative elements of an array. Here, look at this version of above program. Program to Display Negative Elements of the Array
Greetings Mate,
A really interesting, clear and easily readable C Programming Tutorial article of interesting and different perspectives' will clap. So much is so well covered here.
Write a program to print Lowest Common Ancestor of given the values of two nodes in a binary search tree. You may assume that both values already exist in the tree.
The program should take the inputs for the binary tree and construct the tree. Once the construction is over, the program takes 2 inputs to find out the LCA of those nodes.
Eg: In the below given tree, the lowest common ancestor of 4 and 14 is 8.
I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.
Kind Regards,
Minny