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.
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();
}
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);
}
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);
}
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++;
}
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;
}