In this example you will learn how to find and count all negative elements in array. How to count negative elements in array using loop in C programming.
C Code: Write a c program to count total number of negative elements in an array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * C program to count total number of negative elements in array */ #include <stdio.h> #define MAX_SIZE 100 // Maximum array size int main() { int arr[MAX_SIZE]; // Declares array of size 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]); } /* * Count total negative elements in array */ 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 of Example C Code:
C program to count negative elements in array