Count and print total number of positive, negative numbers and zero in array

 


#include<stdio.h>
main()
{
  int   a[100],n,i,size,posi,zcount=0,pcount=0,ncount=0;

  printf("enter the size of an array ");
  scanf("%d",&n);

  printf("enter the elements of array :\n");
  for ( i = 0; i < n; i++)
  {
    scanf("%d",&a[i]);
  }
  
  for ( i = 0; i < n; i++)
  {
      if (a[i]==0)
      {
          zcount=zcount+1;
      }

      else if (a[i]<0)
      {
         ncount=ncount+1;
      }

      else
      {
          pcount=pcount +1;
      }
      
  }
  
 printf("there are %d zero numbers in the array \n ",zcount );
 
 printf("there are %d positive numbers in the array \n ",pcount );
  
 printf("there are %d negative numbers in the array \n ",ncount );


 return 0;

}

output :
enter the size of an array 9 enter the elements of array : 2 4 0 0 5 -4 -6 -9 7
there are 2 zero numbers in the array
there are 4 positive numbers in the array
there are 3 negative numbers in the array

Comments

Popular Post

Write the definition for a class called complex that has floating point data members for storing real and imaginary parts. The class has the following member functions: void set(float, float) to set the specified value in object void disp() to display complex number object complex sum(complex) to sum two complex numbers & return complex number 1. Write the definitions for each of the above member functions. 2. Write main function to create three complex number objects. Set the value in two objects and call sum() to calculate sum and assign it in third object. Display all complex numbers.