C++ Program to Count Positive, Negative and Zeros in Array
Write C++ Program to Count Positive, Negative and Zeros in Array
// CPP Program to Count Positive, Negative and Zeros in Array #include <iostream> using namespace std; int main() { int A[]={3, 5, 12, 89, 65, 45, 32, 0, -9, 0, -89, 0, 0}; int Pos = 0, Neg = 0, Zero = 0, i, n; n = sizeof(A)/sizeof(A[0]); cout << "Array Elements :--> "; for(i=0; i<n; i++) cout << A[i] << " "; for(i = 0; i < n; i++) { if(A[i] == 0) Zero++; else if(A[i] > 0) Pos++; else Neg++; } cout << "\n\nNumber of Zeros: --> " << Zero; cout << "\nNumber of Positive: --> " << Pos; cout << "\nNumber of Negative: --> " << Neg; return 0; }
Output:
Array Elements :--> 3 5 12 89 65 45 32 0 -9 0 -89 0 0
Number of Zeros: --> 4
Number of Positive: --> 7
Number of Negative: --> 2