C program to Calculate the number of positive and negative numbers in an Array

Program-to-find-Number-of-positive-and-negative-number- in-an-array-in-c

Program Description:

Write a Program to count the Number of positive and negative numbers in an array in the C programming language. The program will accept an array from the user and then displays the total number of Positives numbers, Negatives numbers, and Zeros on the console.

Example Input and Output:

Input:

Output:

Prerequisites:

It is recommended to go through the following prerequisites to better understand the program.

The number of positive and negative numbers in an array Program Explanation:

  1. Create an array named numbers with size 100
  2. Also, initialize the other variables like size, nPositive, nNegative, and nZeros variables with Zero.
  3. Take the size of the array from the user. and update the size variable.
  4. Now, Prompt the user to provide the elements to fill the array. Use a for loop to iterate over the numbers array. Start the loop from zero(0) and run it till the size-1 and update each element using the scanf function.
  5. To count the number of positive and negative and zeros in the given array. We need to traverse the array element by element and check if it is positive or negative or zero.
  6. Create another for loop to iterate over the array. The loop starts from Zero and goes till size-1
    • Check if the number[i] is positive, If it is true, Then increment the nPositive variable.
    • If the number[i] is a Negative number, Then increment the nNegative variable.
    • If the above two conditions are false, Then the number must be Zero(0). So increment the nZeros variable.
  7. Once the above loop is completed, The variables nPositive, nNegative, and nZeros contains the number of positive, negative, and zeros in the array.
  8. Display the results on the console using printf function.

Number of positive and negative numbers in an array Algorithm:

  1. Declare the array numbers[100] and The i, size variables.
  2. Initialize nPositive = 0, nNegative = 0, nZeros = 0; variables
  3. Take the desired array size from user
  4. Prompt the user to provide the numbers for array elements.
    • Loop from Zero(0) to size-1 using for loop.
    • Read the value from the user and update the numbers[i] element
  5. Create another for loop to traverse the array.
    • Loop from Zero(0) to size-1 using for loop.
    • If the numbers[i] > 0 is true, Increment nPositive – i.e nPositive++;
    • else if the numbers[i] < 0 is true, Increment nNegative variable – i.e nNegative++;
    • else increment nZeros variable – i.e nZeros++;
  6. Display the results on standard output.

Program to calculate Number of positive and negative numbers in an Array:

Here is the C Code for the above count number of positive and negative numbers in the array algorithm.

Program Output:

Compile and Run the program.

Test 1:

Number-of-positive-and-negative-number- in-an-array-in-c

Test 2: Try a few more examples

As we can see from the above output, The program is providing the number of positive numbers, Negative numbers, and Zeroes in the array.

Number of positive and negative numbers in an array using a user-defined function:

Let’s convert the above program to use the user-defined functions instead of using only one function (i.e main())

The following program is a rewritten version of the above program, which uses four user-defined functions to do a specific task.

For example, The read function is used to take the user input and update the array. and The getNPositives() function takes the input array and returns the number of positive numbers in the array. Similarly, the other two functions also return negative numbers and zeros.

We have defined Four functions in the above program. Here are the details of these functions.

  1. The read() function – void read(int numbers[], int size)
    • read() function takes two formal arguments and Array and it’s size and updates it with the user input.
  2. The getNPositives() function – int getNPositives(int numbers[], int size)
    • getNPositives() function takes two formal arguments, numbers[] array and its size
    • The getNPositives() function calculates the number of positive numbers in the numbers array and returns it back to the caller.
  3. The getNNegatives() function – int getNNegatives(int numbers[], int size)
    • getNNegatives() function takes two formal arguments, numbers[] array and its size
    • The getNNegatives() function calculates the number of negative numbers in the numbers array and returns it back to the caller.
  4. The getNZeros() function – int getNZeros(int numbers[], int size)
    • getNZeros() function takes two formal arguments, numbers[] array and its size
    • The getNZeros() function calculates the number of zeros in the numbers array and returns it back to the caller.

Program Output:

Compile and run the program and see the results.

program-to-count-number-of-positive-negative-and-zeros-in-c

As expected, The program is providing the desired results.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to calculate the total number of Positive numbers, Negative numbers, and Zeros in an Array […]

Leave a Reply