C Program to count positive, negative and zero numbers in array

Category: C Program

C Program to count positive, negative and zero numbers in an array

C Program to print positive, negative and zero numbers in array

#include <stdio.h>

void main()
{
    int arr[50], n, i, positive = 0, negative = 0, zero = 0;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter array elements-\n");
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    for (i = 0; i < n; i++)
    {
        if (arr[i] > 0)
            positive++;
        else if (arr[i] < 0)
            negative++;
        else
            zero++;
    }

    printf("\nNumber of positive elements: %d\n", positive);
    printf("Number of negative elements: %d\n", negative);
    printf("Number of zeros: %d", zero);
}

Output

Enter number of elements: 5
Enter array elements-
-3
-5
4
1
0
Number of positive elements: 2
Number of negative elements: 2
Number of zeros: 1

Recommended Posts