C Program to Count Positive and Negative Numbers in an Array

How to write a C Program to Count Positive and Negative Numbers in an Array using For Loop, While Loop, and Functions with example.

C Program to Count Positive and Negative Numbers in an Array

This program allows the user to enter the Size and the row elements of One Dimensional Array. Next, we are using For Loop to iterate the array values and check for the Positive and Negative Numbers

```/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>

int main()
{
int Size, i, a[10];
int Positive_Count = 0, Negative_Count = 0;

printf("\n Please Enter the Size of an Array :  ");
scanf("%d", &Size);

for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}

for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
Positive_Count++;
}
else
{
Negative_Count++;
}
}

printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
return 0;
}```

Here, For Loop will make sure that the number is between 0 and maximum One Dimensional Array size value. In this example, it will be from 0 to 4

`for(i = 0; i < Size; i ++)`

In the Next line, We declared the If statement

`if(a[i] >= 0)`

Any number that is greater than or equal to 0 is a Positive Number. If condition will check for the same.

• If the condition is True, it is a Positive Number, and the C Programming compiler will increment a Positive_Count.
• If the condition is False, it is a Negative Number, and compiler will increment Negative_Count.

Program to Count Positive and Negative Numbers in an Array using While Loop

This program is the same as above, but this time we used While Loop to count Positive and Negative Numbers in an array

```/* C Program to Count Positive and Negative Numbers in an Array */
#include<stdio.h>

int main()
{
int Size, i, j = 0, a[10];
int Positive_Count = 0, Negative_Count = 0;

printf("\n Please Enter the Size of an Array :  ");
scanf("%d", &Size);

for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}

while(j < Size)
{
if(a[j] >= 0)
{
Positive_Count++;
}
else
{
Negative_Count++;
}
j++;
}

printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
return 0;
}```
``````Please Enter the Size of an Array :  7

10 -56 -98 -78 0 -14 10

Total Number of Positive Numbers in this Array = 3
Total Number of Negative Numbers in this Array = 4``````

Program to Count Positive and Negative Numbers in an Array using Functions

This program is the same as the first example. But we separated the logic to count positive numbers and negative numbers using Functions.

```/* C Program to Count Positive and Negative Numbers in an Array */

#include<stdio.h>

int CountPositiveNumbers(int a[], int Size);
int CountNegativeNumbers(int a[], int Size);

int main()
{
int Size, i, a[10];
int Positive_Count = 0, Negative_Count = 0;

printf("\n Please Enter the Size of an Array  :  ");
scanf("%d", &Size);

printf("\nPlease Enter the Array Elements :  ");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}

Positive_Count = CountPositiveNumbers(a, Size);
Negative_Count = CountNegativeNumbers(a, Size);

printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count);
printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count);
return 0;
}

int CountPositiveNumbers(int a[], int Size)
{
int i, Positive_Count = 0;
printf("\n List of Positive Numbers in this Array: ");

for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
printf("%d  ", a[i]);
Positive_Count++;
}
}
return Positive_Count;
}

int CountNegativeNumbers(int a[], int Size)
{
int i, Negative_Count = 0;
printf("\n List of Negative Numbers in this Array: ");

for(i = 0; i < Size; i ++)
{
if(a[i] < 0)
{
printf("%d  ", a[i]);
Negative_Count++;
}
}
return Negative_Count;
}```
``````Please Enter the Size of an Array  :  10

Please Enter the Array Elements :  -12 0 -18 -16 106 125 -98 12 145 -12

List of Positive Numbers in this Array: 0  106  125  12  145
List of Negative Numbers in this Array: -12  -18  -16  -98  -12
Total Number of Positive Numbers in this Array = 5
Total Number of Negative Numbers in this Array = 5``````