C program to count numbers of positive and negative numbers

Write a C program using arrays to count the numbers of positive and negative numbers which accepts the inputs as "size of the array" & "elements of the array" and outputs as "number of negative numbers in an array are" & "numbers of positive numbers in an array are" ?

Showing Answers 1 - 7 of 7 Answers

baseersd

  • Oct 31st, 2007
 

Code
  1. #include

  2. int main()

  3. {

  4.     int p_nArr[20]={0};    

  5.     int nCount_Pos = 0;

  6.     int nCount_Neg = 0;

  7.     int num=0;

  8.     int SIZEOFARRAY= 0;

  9.     int index;

  10.     printf("nEnter the Size of the array");

  11.     scanf("%d",&SIZEOFARRAY);

  12.     printf("nEnter the elements ::n");

  13.     for( index = 0; index < SIZEOFARRAY; index++ )

  14.     {

  15.          scanf("%d",&p_nArr[index]);

  16.          if (p_nArr[index] > 0 )

  17.             nCount_Pos++ ;

  18.          else if (p_nArr[index] < 0 )

  19.              nCount_Neg++;

  20.     }

  21.     if ( nCount_Neg > 0)

  22.     {

  23.        printf("nNumber of negative numbers in the array are :: %dn", nCount_Neg);

  24.        for( index = 0; index < SIZEOFARRAY; index++ )      

  25.        {

  26.           if (p_nArr[index] < 0 )  

  27.          printf("%d ",p_nArr[index]);            

  28.        }

  29.     }

  30.     if ( nCount_Pos > 0 )

  31.     {

  32.            printf("nNumber of Positive numbers in the array are :: %dn",nCount_Pos);

  33.            for( index = 0; index < SIZEOFARRAY; index++ )      

  34.            {

  35.               if (p_nArr[index] > 0 )  

  36.              printf("%d ",p_nArr[index]);            

  37.            }          

  38.     }

  39.  

  40.     getch();

  41.  

  42. }

  43.  

  44. }



//Note : This program can be solved efficiently using linked list.

  Was this answer useful?  Yes

kaushalgoa

  • Dec 27th, 2007
 

Code
  1. void func(int num, int * arr, int *pos, int *neg)

  2. {

  3.     int i = 0;

  4.     *pos = 0;

  5.     *neg = 0;

  6.     for(i = 0; i < num; i++)

  7.     {

  8.         if(*(arr + i) >= 0)

  9.             (*pos)++;

  10.         else

  11.             (*neg)++;

  12.     }

  13.     return;

  14. }

  15.  

  16. void main()

  17. {

  18.    

  19.     int array[] = {1, 2, 3, 4, 5, -4, -6, -8, 0};

  20.     int pos, neg;

  21.     func(9, array, &pos, &neg);

  22.     return;

  23.    

  24. }

  Was this answer useful?  Yes

jintojos

  • Jun 18th, 2008
 

Code
  1.  

  2. #include<stdio.h>

  3. #include<conio.h>

  4.  

  5. void main()

  6.  {

  7.     int *arr,num,i;

  8.     clrscr();

  9.     printf("Enter The Number Of Elements :");

  10.     scanf("%d",&num);

  11.     arr=(int *)malloc(num*2);

  12.     printf("Enter The numbers :");

  13.     for(i=0;i<num;i++) scanf("%d",(arr+i));

  14.     printf("nPositive Numbers Are....n");

  15.     for(i=0;i<num;i++) if(arr[i]>0) printf(" %d",arr[i]);

  16.     printf("nNegative Numbers Are....n");

  17.     for(i=0;i<num;i++) if(arr[i]<0) printf(" %d",arr[i]);

  18.     getch();

  19.  }

  20.  

  21.  

  Was this answer useful?  Yes

jintojos

  • Jun 18th, 2008
 

Code
  1.  

  2. #include<conio.h>

  3. #include<stdio.h>

  4.  

  5. void main()

  6.   {

  7.       int *arr,num,i=0,pos=0,neg=0;

  8.       clrscr();

  9.       printf("Enter The Number Of Elements :");

  10.       scanf("%d",&num);

  11.       arr=(int *)malloc(num*2);

  12.       printf("Enter The Numbers :");

  13.       for(i=0;i<num;i++)

  14.          {

  15.             scanf("%d",arr+i);  

  16.             if(arr[i]>0) pos++;

  17.                 else neg++;

  18.          }

  19.       printf("The Number Of Positive Numbers Are :%d",pos);

  20.       printf("The Number Of Negative Numbers Are :%d",neg);

  21.       printf("Number Of Zeros Are :%d",num-(pos+neg));

  22.       getch();

  23.   }

  24.  

  25.  

  Was this answer useful?  Yes

kesineni

  • Jul 12th, 2009
 

Code
  1.  

  2. #include <stdio.h>

  3.  

  4. main()

  5. {

  6.    int a[] = { 1,-3,-5,-4,-9,2,8};

  7.    int len,i,val;

  8.    int npos=0, nneg=0;

  9.  

  10.    len = sizeof(a)/sizeof(a[0]);

  11.    printf("%dn",len);

  12.  

  13.    for( i=0; i< len; i++)

  14.    {

  15.        val = a[i] & 0x10000000;

  16.        printf("%dn",val);

  17.        if (val > 0)

  18.           nneg++;

  19.        else

  20.           npos++;

  21.    }

  22.  

  23.    printf("npos=%d  nneg=%dn", npos, nneg);

  24.  

  25.   }

  26.  

  27.  

asdasda

  • Aug 10th, 2013
 

Code
  1. #include<conio.h>

  2. #include<iostream>

  3.  

  4. using namespace std;

  5.  

  6. int value(int a);

  7.  

  8. int main ()

  9. {

  10.     int a, num, count=0;

  11.    

  12.      cout<<"How many values: ";

  13.      cin>>num;

  14.    

  15.     getch ();

  16.    

  17.    

  18.    

  19. int value(int a);

  20.    

  21.    

  22.     for (a=1;a<=num;a++)

  23.     {

  24.    

  25.         cout<<"Enter value "<<a<<": ";

  26.         cout<<endl;

  27.         }

  28.        

  29.        

  30.         getch ();

  31.         return 0;

  32.         }

  33.        

  34.  

  Was this answer useful?  Yes

Code
  1. #include<iostream>

  2. #include<stdio.h>

  3. using namespace std;

  4. int main(){

  5. cout<<"enter the size of array"<<endl;

  6. int size,count_neg=0,count_pos=0;

  7. cin>>size;

  8. int a[size];

  9. for(int i=0;i<size;i++){

  10. cout<<"enter element of a["<<i<<"]=";

  11. cin>>a[i];

  12. if((a[i]>>31)==-1) count_neg++;

  13. else count_pos++;

  14. }

  15. cout<<"total negative numbers "<<count_neg<<" and positive number is "<<count_pos;

  16.  

  17. return 0;

  18. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions