Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 16-01-2010
Member
 
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.

Reply With Quote
  #2  
Old 16-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
Re: Need C program code to count numbers of negative & positive numbers

Refer following program to count positive and negative numbers:
Quote:
#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();
}
Reply With Quote
  #3  
Old 16-01-2010
Member
 
Join Date: May 2008
Posts: 2,007
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:
Quote:
#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);

}
Reply With Quote
  #4  
Old 16-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
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:
Quote:
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);
}
Reply With Quote
  #5  
Old 16-01-2010
Member
 
Join Date: May 2008
Posts: 2,290
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.
Quote:
for(Ik=0;Ik < nk;Ik++)
{

if(ak[Ik] < 0)

countk_neg++;

else

countk_pos++;

}
Reply With Quote
  #6  
Old 16-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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:
Quote:
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;

}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Need C program code to count numbers of negative & positive numbers"
Thread Thread Starter Forum Replies Last Post
How to change negative numbers to positive numbers in Excel Shaina Na Microsoft Project 3 08-01-2012 04:35 PM
How can i count numbers on odd/even places? cowa Software Development 1 17-11-2009 07:43 AM
How to count numbers from a table in C language MKAIF Software Development 3 01-10-2009 10:34 PM
Automatically display negative numbers in red in Excel 2007 SpearMan Tips & Tweaks 2 19-03-2009 11:10 PM
SQL Query for Searching Missing Numbers from Sequence of Numbers Bhagwandas Software Development 3 18-02-2009 12:47 PM


All times are GMT +5.5. The time now is 01:31 PM.