C Program To Count Number of Positive, Negative and Zeros In An Array

Lets write a C program to count number of positive, negative and zeros in an Array.

Logic

number scale
If input number is greater than 0, then its positive number. If input number is less than 0, then its negative. If its neither greater than 0, nor less than 0, then the input number must be 0.

Related Read:
Number is Positive or Negative or Zero: C Program

Example: Expected Input/Output

Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4

Positive no: 4
Negative no: 5
Zeros: 1

Visual Representation

count positive negative zero in an array

Video Tutorial: C Program To Count Number of Positive, Negative and Zeros In An Array


[youtube https://www.youtube.com/watch?v=Z1mES-0NM-E]

YouTube Link: https://www.youtube.com/watch?v=Z1mES-0NM-E [Watch the Video In Full Screen.]

Source Code: C Program To Count Number of Positive, Negative and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, p = 0, n = 0, z = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] > 0)
            p++;
        else if(a[i] < 0)
            n++;
        else
            z++;
    }

    printf("\nPositive no: %d\nNegative no: %d\nZeros: %d\n", p, n, z);

    return 0;
}

Output:
Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4

Positive no: 4
Negative no: 5
Zeros: 1

Logic To Find Number of Positive, Negative and Zeros In An Array

First we initialize 0 to variables p, n and z – which represents positive, negative and zero. We accept 10 integer numbers from the user. Next we iterate through the array using for loop and check if the fetched array element is greater than 0, which means its a positive number, so we increment the value of variable p by one. If fetched array element is less than 0, which means its a negative number, so we increment the value of variable n by one. If both the cases fail, then the number must be zero, so we increment the value of z by 1.

After the completion of for loop execution, variables p, n and z will have the number of positive, negative and zeros present in the array.

Method 2

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, p = 0, n = 0, z = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] > 0)
            p++;
        else if(a[i] < 0)
            n++;
        else
            z++;
    }

    printf("\nPositive no: %d\nNegative no: %d\nZeros: %d\n", p, n, z);

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
0
-1
-2
-3
-4
-5

Positive no: 4
Negative no: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is greater than 0 or less than zero or is equal to zero, and increment the values of variable p, n and z accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. Less overhead and more efficient.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert