C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Count positive, negative, and zero elements in an array

C Program: Count positive, negative, and zero elements in an array

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i;

    int positiveCount = 0, negativeCount = 0, zeroCount = 0;

 

    // Input size of the array

    printf("Enter number of elements in the array: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Count positive, negative, and zero elements

    for (i = 0; i < n; i++) {

        if (arr[i] > 0)

            positiveCount++;

        else if (arr[i] < 0)

            negativeCount++;

        else

            zeroCount++;

    }

 

    // Display results

    printf("\nTotal positive numbers: %d", positiveCount);

    printf("\nTotal negative numbers: %d", negativeCount);

    printf("\nTotal zeros: %d\n", zeroCount);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements in the array: 7
Enter 7 elements:
5 -3 0 12 -7 0 9

OUTPUT :
Total positive numbers: 3
Total negative numbers: 2
Total zeros: 2

Explanation

  1. The user enters the size and elements of the array.
  2. A for loop checks each element:
    • If it’s greater than 0 → positive.
    • If it’s less than 0 → negative.
    • If it’s equal to 0 → zero.
  3. The counts are displayed at the end.