Count Positive Negative and Zeros in the Array

Program description:- Write a C program to count the positive negative and zeros in given array.

Take an array and check each element of the array, if the element is greater than zero then it is positive number, or if it is less than zero then it is a negative number, else it is zero. Take three count variables and update them based on the condition.

int main()
   int a[100], n, i, positive=0, negative=0, zero=0;

   printf("Enter array size [1-100]: ");
   scanf("%d", &n);
   printf("Enter %d elements: ",n);

   for(i=0; i<n; i++)
     scanf("%d", &a[i]);
     if(a[i]>0) positive++;
     else if(a[i]==0) zero++;
     else negative++;

   printf("Positive number count is %d", positive);
   printf("\nNegative number count is %d", negative);
   printf("\nZeros count is %d", zero);
   return 0;

Output for the different test-cases:-

Enter array size [1-100]: 5
Enter 5 elements: 10 20 0 -5 -9
Positive number count is 2
Negative number count is 2
Zeros count is 1

Enter array size [1-100]: 6
Enter 6 elements: 0 0 2 30 -6 2
Positive number count is 3
Negative number count is 1
Zeros count is 2

Using if-else-if condition we can find the number is greater than zero or equal to zero or less than zero. If the number is greater than zero than variable positive is incremented. Similarly happens with variable negative and zero. When the number is equal to zero than variable zero is incremented. When the number is lesser than zero than variable negative is incremented.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Similar C programming examples on Arrays

Leave a Comment

Your email address will not be published. Required fields are marked *