Count Positive and Negative Numbers
C++
Easy
7 views
Problem Description
Given an array with positive and negative numbers, count how many are positive and how many are negative. This builds conditional logic skills.
Logic: Check each number's sign and increment respective counter
Official Solution
void question4_count_pos_neg() {
int arr[] = {5, -3, 8, -1, 0, -7, 12};
int size = 7;
int positive = 0, negative = 0;
for(int i = 0; i < size; i++) {
if(arr[i] > 0) {
positive++;
} else if(arr[i] < 0) {
negative++;
}
}
cout << "Positive: " << positive << ", Negative: " << negative << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!