Count Positive and Negative Numbers in an Array in C

To count positive and negative numbers in an array in C, we need to iterate through the array, check each number, and maintain separate counters for positive and negative values. By using a loop and conditional statements, we can efficiently determine the count of positive and negative numbers.


Examples to Count Positive and Negative Numbers

1. Counting Positive and Negative Numbers Using a for Loop

In this example, we will use a for loop to iterate through an array of integers and count the number of positive and negative numbers.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {10, -5, 8, -3, 6, -1, 7, -9};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int positiveCount = 0, negativeCount = 0;

    // Iterate over the array and count positives and negatives
    for (int i = 0; i < size; i++) {
        if (numbers[i] > 0) {
            positiveCount++;
        } else if (numbers[i] < 0) {
            negativeCount++;
        }
    }

    printf("Positive numbers: %d\n", positiveCount);
    printf("Negative numbers: %d\n", negativeCount);

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] with both positive and negative numbers.
  2. We calculate the array size using sizeof(numbers) / sizeof(numbers[0]).
  3. We initialize positiveCount and negativeCount to zero.
  4. Using a for loop, we iterate over the array.
  5. Inside the loop, we check if the number is greater than zero and increment positiveCount.
  6. If the number is less than zero, we increment negativeCount.
  7. Finally, we print the counts of positive and negative numbers.

Output:

Positive numbers: 4
Negative numbers: 4

2. Counting Positive and Negative Numbers Using a while Loop

In this example, we will use a while loop instead of a for loop to count positive and negative numbers in an array.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {12, -7, 5, -2, 9, -4};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int positiveCount = 0, negativeCount = 0, i = 0;

    // Iterate using while loop
    while (i < size) {
        if (numbers[i] > 0) {
            positiveCount++;
        } else if (numbers[i] < 0) {
            negativeCount++;
        }
        i++;
    }

    printf("Positive numbers: %d\n", positiveCount);
    printf("Negative numbers: %d\n", negativeCount);

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] with some positive and negative values.
  2. We determine the number of elements using sizeof().
  3. We initialize positiveCount and negativeCount to zero.
  4. We initialize i to zero before starting the while loop.
  5. The while loop runs until i is less than size.
  6. Inside the loop, we check if numbers[i] is positive or negative and update the respective counters.
  7. Finally, we print the count of positive and negative numbers.

Output:

Positive numbers: 3
Negative numbers: 3

Conclusion

In this tutorial, we explored how to count positive and negative numbers in an array using different looping techniques:

  1. Using a for loop: Suitable when we have a known number of elements.
  2. Using a while loop: Useful when we prefer condition-based iteration.