Results 1 to 6 of 6

Thread: Need C program code to count numbers of negative & positive numbers

  1. #1
    Join Date
    Dec 2009
    Posts
    41

    Need C program code to count numbers of negative & positive numbers

    Hi All,

    I have to write one C program code to count numbers of negative & positive numbers. Suppose we provide three positive and five negative number then output result should show the count of positive number as three and count of negative number as a five. I have one program for this but it is not giving proper result. If you have C program code to count numbers of negative & positive numbers, then please share with me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    Re: Need C program code to count numbers of negative & positive numbers

    Refer following program to count positive and negative numbers:
    #include<conio.h>
    #include<stdio.h>

    void main()
    {
    int *arrq numq iq;

    printf("Enter The Number Of Elements :");
    scanf(" dq" &numq);
    arr (int *)malloc(num*2);
    printf("Enter The numbers :");
    for(iq 0;iq<num;i++) scanf(" dq" (arrq+iq));

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

    for(iq 0;iq<numq;i++) if(arrq[iq]>0) printf(" dq" arrq[iq]);

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

    for(iq 0;iq<numq;iq++) if(arrq[iq]<0) printf(" dq" arrq[iq]);
    getch();
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,015

    Re: Need C program code to count numbers of negative & positive numbers

    Hi,

    Have you tried below code to count positive and Negative number ? If not then try this code:
    #include<stdio.h>
    #include<conio.h>

    void main()

    {
    int *arrb num ib,posb,negb;

    printf("Enter The Number Of Elements :");
    scanf(" d" &numb);
    arr (int *)malloc(numb*2);
    printf("Enter The Numbers :");
    for(ib=0;ib<numb;ib++)
    {
    scanf(" d" arrb+ib);
    if(arrb[ib]>0) posb++;
    else negb++;
    }
    printf("The Number Of Positive Numbers: d" posb);
    printf("The Number Of Negative Numbers: d" negb);

    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,010

    Re: Need C program code to count numbers of negative & positive numbers

    Hi friend,

    To code the program to print the positive and negative number. You first need to code the logic to determine the positive and negative umber. And then implement the logic to count that numbers. For reference see below code:
    void main( )
    {

    int ay[50],ny,county_neg=0,county_pos=0,I;

    printf(“Enter the size of the array\n”);
    scanf(“%d”,&ny);

    printf(“Enter the elements of the array\n”);

    for Iy=0;Iy < ny;Iy++)
    scanf(“%d”,&ay[Iy]);

    for(Iy=0;Iy < ny;Iy++)
    {

    if(ay[Iy] < 0)

    county_neg++;

    else
    county_pos++;

    }
    printf(“There are %d negative numbers in the
    array\n”,county_neg);

    printf(“There are %d positive numbers in the
    array\n”,county_pos);
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,302

    Re: Need C program code to count numbers of negative & positive numbers

    You need to use the below for loop code in your program code. This for loop comprise the code to count the positive as well as the negative numbers. Inside for loop I have use one If loop to test the condition for positive and negative number. Try this code and let me know your output.
    for(Ik=0;Ik < nk;Ik++)
    {

    if(ak[Ik] < 0)

    countk_neg++;

    else

    countk_pos++;

    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,394

    Re: Need C program code to count numbers of negative & positive numbers

    Hello friend,

    Try out this code, when you run this program you get the count of negative and positive number as a output result:
    void funcv(int numv int * arrv int *posv int *negv)
    {
    int iv 0;
    *posv 0;
    *negv 0;
    for(iv= 0; iv < numv; iv++)
    {
    if(*(arrv + iv) > 0)
    (*posv)++;
    else
    (*negv)++;
    }
    return;
    }

    void main()
    {

    int array[] {1 2 3 4 5 -4 -6 -8 0};
    int posv negv;
    func(&posv, &negv);
    return;

    }

Similar Threads

  1. How to change negative numbers to positive numbers in Excel
    By Shaina Na in forum Microsoft Project
    Replies: 3
    Last Post: 08-01-2012, 04:35 PM
  2. How can i count numbers on odd/even places?
    By cowa in forum Software Development
    Replies: 1
    Last Post: 17-11-2009, 07:43 AM
  3. How to count numbers from a table in C language
    By MKAIF in forum Software Development
    Replies: 3
    Last Post: 01-10-2009, 10:34 PM
  4. Replies: 2
    Last Post: 19-03-2009, 11:10 PM
  5. SQL Query for Searching Missing Numbers from Sequence of Numbers
    By Bhagwandas in forum Software Development
    Replies: 3
    Last Post: 18-02-2009, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •