C Program to Count Negative Numbers in an Array

  • Write a C program to count all negative numbers of an array.

Required Knowledge


Algorithm to count negative numbers in an array
Let inputArray is an integer array having N elements.
  • Using a for loop, traverse inputArray from index 0 to N-1.
  • For every element inputArray[i], check whether it is negative number or not(inputArray[i] < 0) and increment the counter accordingly.

C program to count number of negative elements in an array

#include <stdio.h>
#include <conio.h>
 
int main(){
    int inputArray[100], elementCount, index, counter=0;
     
    printf("Enter Number of Elements in Array\n");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
    
    /* Read array elements */
    for(index = 0; index < elementCount; index++){
        scanf("%d", &inputArray[index]);
    }
       
    /* Iterate form index 0 to elementCount-1 and 
 check for negative numbers */
    for(index = 0; index < elementCount; index++){
        if(inputArray[index] < 0) {
            counter++;
        }
    }
    
    printf("Number of Negative Elements in Array : %d\n", counter);
    getch();
    return 0;
}
Output
Enter Number of Elements in Array
8
Enter 8 numbers
2 -4 9 10 0 -5 -1 1
Number of Negative Elements in Array : 3

Related Topics
C Program to print all negative numbers of an array
C Program to find sum of all elements of an array
C program to find maximum and minimum element of an array
C Program to print unique elements of an unsorted array
C Program to search an element in an array
C program to reverse an array
C Program to sort elements of an array in decreasing order
C Program to find count of each element of an array
C Program to find maximum elements in an array
List of all array programs