Advertisement
Shailrshah

Counting number of +ve,-ve and non-zero terms

Apr 18th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. int count=0,p=0,n=0;
  3. void nzero()
  4. {
  5.     count++;
  6. }
  7. void positive()
  8. {
  9.     p++;
  10. }
  11. void negative()
  12. {
  13.     n++;
  14. }
  15.  
  16. int main()
  17. {
  18.     int num, choice = 1;
  19.     do
  20.     {
  21.         printf("Enter a number.");
  22.         scanf("%d",&num);
  23.         if(num!=0)
  24.         {
  25.             nzero();
  26.             if(num>0)
  27.                 positive();
  28.             else
  29.                 negative();
  30.         }
  31.         printf("Do you want to enter more? 1.Yes 2.No: ");
  32.         scanf("%d",&choice);
  33.     } while (choice!=2);
  34.     printf("\nNumber of non-zero numbers: %d\n",count);
  35.     printf("Number of positive numbers: %d\n",p);
  36.     printf("Number of negative numbers: %d\n",n);
  37.     return 0;
  38. }
  39. //Output
  40. //Enter a number.-2
  41. //Do you want to enter more? 1.Yes 2.No: 1
  42. //Enter a number.0
  43. //Do you want to enter more? 1.Yes 2.No: 1
  44. //Enter a number.34
  45. //Do you want to enter more? 1.Yes 2.No: 2
  46.  
  47. //Number of non-zero numbers: 2
  48. //Number of positive numbers: 1
  49. //Number of negative numbers: 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement