C++ Program to Count Zeros, Positive and Negative Numbers

In this C++ program, we will count number of positive numbers, negative numbers and zeroes in an array. Here we will use if-else statement to compare whether a number is positive, negative or zero.

In below program, we first ask user to enter number of elements in array and store it in count variable. Then we ask user to enter array elements and store then in an integer array "input". Using a for loop, we traverse input array from index 0 to count-1 and compare each array element to check whether it is positive, negative or zero.

We are using nCount, pCount and zCount variables to count the number of positive, negative and zeroes respectively. Finally, we print the count of zeroes, positive and negative numbers on screen using cout.

C++ Program to Count Zeros, Positive and Negative Numbers

// C++ Program to count positive negative and zero numbers 
  
#include <iostream>
using namespace std;

int main(){
    int input[100], count, i, nCount=0, pCount=0, zCount=0;
      
    cout << "Enter Number of Elements in Array\n";
    cin >> count;
    
    cout << "Enter " << count << " numbers \n";
     
    // Read elements 
    for(i = 0; i < count; i++){
        cin >> input[i];
    }
        
    // Iterate form index 0 to elementCount-1 and 
     // check for positive negative and zero 
    for(i = 0; i < count; i++){
        if(input[i] < 0) {
            nCount++;
        } else if(input[i] > 0) {
            pCount++;
 } else {
     zCount++;
 }
    }
     
    cout << "Negative Numbers : " << nCount << endl;
    cout << "Positive Numbers : " << pCount << endl;
    cout << "Zeroes : " << zCount << endl;
    
    return 0;
}
Output
Enter Number of Elements in Array
6
Enter 6 numbers
4 -3 0 8 -2 10
Negative Numbers : 2
Positive Numbers : 3
Zeroes : 1