In this article, we are going to learn How to count negative elements array in C++to count all negative elements in that array.
How to count negative elements array in C++
#include <iostream>
#define MAX_SIZE 250
using namespace std;
int main()
{
int Array[MAX_SIZE];
int Size;
int count = 0;
cout<<"Please enter size of the array (Max 250): ";
cin>>Size;
cout<<"Please enter elements in array: ";
for(int i=0; i<Size; i++)
{
cin>>Array[i];
}
cout<<"All negative elements in array are:";
for(int i=0; i<Size; i++)
{
if(Array[i] < 0)
{
count++;
}
}
cout<<"Total number of negative elements: "<<count;
return 0;
}
Output
Please enter size of the array (Max 250): 9
Please enter elements in array: 12
-13
-14
-79
80
45
60
70
80
All negative elements in array are:Total number of negative elements: 3