Here's a C program to display only the negative elements of the array using for loops and IF conditions.
# 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 negative 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 negative elements are :
-20 -40 -50
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 negative 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 less than 0 i.e. negative 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 positive elements of an array. Here, look at this version of above program. Program to Display Positive Elements of the Array.
6. Hi There,
Program to Display Negative Elements of the Array in C | C Programbeing contrived to exist for many projects simply so it can be run will be the first to hit the wall, but those projects where the functions to make existing transactions cheaper in real world applications will find the elusive real world demand.
I have written some code to calculate the number of rolls of wallpaper to decorate a room.
I have compiled it and at first it worked OK, using it in my C compiler program (LCC Wedit).
Subsequently I tried running it by clicking on the executable and it does not run and returns code -1 instead of 0. Even if I return to the compiler program it comes up with the same return code.
Why is this happening?
Great effort, I wish I saw it earlier. Would have saved my day :)
Merci Beaucoup,
Preethi
Greetings Mate,
Brilliant article, glad I slogged through the #topic it seems that a whole lot of the details really come back to from my past project.
why is saying c programming is a based on java?
Very useful article, if I run into challenges along the way, I will share them here.
Obrigado,