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:
- We declare an integer array
numbers[]with both positive and negative numbers. - We calculate the array size using
sizeof(numbers) / sizeof(numbers[0]). - We initialize
positiveCountandnegativeCountto zero. - Using a
forloop, we iterate over the array. - Inside the loop, we check if the number is greater than zero and increment
positiveCount. - If the number is less than zero, we increment
negativeCount. - 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:
- We declare an integer array
numbers[]with some positive and negative values. - We determine the number of elements using
sizeof(). - We initialize
positiveCountandnegativeCountto zero. - We initialize
ito zero before starting thewhileloop. - The
whileloop runs untiliis less thansize. - Inside the loop, we check if
numbers[i]is positive or negative and update the respective counters. - 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:
- Using a
forloop: Suitable when we have a known number of elements. - Using a
whileloop: Useful when we prefer condition-based iteration.
