Count the total number of negative elements in an array with C.
#include <stdio.h>
int main()
{
int arr[100];
int i, size, count = 0;
/* Input size of array */
printf("Enter size of the array : ");
scanf("%d", &size);
/* Input array elements */
printf("Enter elements in array : ");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++)
{
/* Increment count if current array element is negative */
if (arr[i] < 0)
{
count++;
}
}
printf("\nTotal negative elements in array = %d", count);
return 0;
}
Output
Enter size of the array : 5
Enter elements in array : -5 4 -7 6 -2
Total negative elements in array = 3
0 Comments