C program to count Positive and Negative numbers in an Array

Given an array arr of integers of size N, the task is to find the count of positive numbers and negative numbers in the array

Examples:

Input: arr[] = {2, -1, 5, 6, 0, -3}
Output:
Positive elements = 3
Negative elements = 2
There are 3 positive, 2 negative, and 1 zero.

Input: arr[] = {4, 0, -2, -9, -7, 1}
Output:
Positive elements = 2
Negative elements = 3
There are 2 positive, 3 negative, and 1 zero.

Approach:



  1. Traverse the elements in the array one by one.
  2. For each element, check if the element is less than 0. If it is, then increment the count of negative elements.
  3. For each element, check if the element is greater than 0. If it is, then increment the count of positive elements.
  4. Print the count of negative and positive elements.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find the count of positive
// and negative integers in an array
  
#include <stdio.h>
  
// Function to find the count of
// positive integers in an array
int countPositiveNumbers(int* arr, int n)
{
    int pos_count = 0;
    int i;
    for (i = 0; i < n; i++) {
        if (arr[i] > 0)
            pos_count++;
    }
    return pos_count;
}
  
// Function to find the count of
// negative integers in an array
int countNegativeNumbers(int* arr, int n)
{
    int neg_count = 0;
    int i;
    for (i = 0; i < n; i++) {
        if (arr[i] < 0)
            neg_count++;
    }
    return neg_count;
}
  
// Function to print the array
void printArray(int* arr, int n)
{
    int i;
  
    printf("Array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
  
// Driver program
int main()
{
    int arr[] = { 2, -1, 5, 6, 0, -3 };
    int n;
    n = sizeof(arr) / sizeof(arr[0]);
  
    printArray(arr, n);
  
    printf("Count of Positive elements = %d\n",
           countPositiveNumbers(arr, n));
    printf("Count of Negative elements = %d\n",
           countNegativeNumbers(arr, n));
  
    return 0;
}

chevron_right


Output:

Array: 2 -1 5 6 0 -3 
Count of Positive elements = 3
Count of Negative elements = 2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.