Count positive and negative numbers in an array
C program to count positive and negative numbers in an array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int arr[MAX];
int i, n;
int count_pos, count_neg;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&arr[i]);
}
count_pos = 0;
count_neg = 0;
for(i=0;i<n;i++)
{
if(arr[i]>=0)
{
count_pos++;
}
else
{
count_neg++;
}
}
printf("\nTotal count of positive numbers\t:%d",count_pos);
printf("\nTotal count of negative numbers\t:%d",count_neg);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements
Element 1 :4
Element 2 :5
Element 3 :-5
Element 4 :-8
Element 5 :7
Element 6 :0
Element 7 :6
Element 8 :-3
Total count of positive numbers :5
Total count of negative numbers :3