C Program to find sum & average of positive and negative numbers in an array

This is an array-related C program that needs to find the sum of all negative numbers and the sum of all positive numbers in an array. Also, find the average of the negative and positive numbers. Here. To understand this finding sum of elements in an Array C program, you should have knowledge of the following C programming topics:

What is an Array in C?

An Array is a collection of elements of the same data type stored in a sequence of memory locations that can be accessed by the array index.

An Array will be declared as data type array_name [size], which is "int array[23]". Arrays use sequential memory locations, so the lowest address or the starting address, also called the base address, has the first element, and the highest address has the last element of the array.

How to calculate the average and sum of positive and negative numbers?

Suppose we have some numbers that have to add to an Array. From that array, we have to calculate the sum of the Positive numbers and Negative numbers separately using the Addition operation. After that, find the Average of the numbers by dividing the sum by the number of elements.

How do we implement calculating the sum of array elements and finding their average in C program?

In this C program, we are adding the user input into an Array, and so we can access the elements in the array by incrementing the array index by one. Now we display the numbers in the array using another for loop. After that, open a for loop to check whether each element is Positive, Negative, or Zero.

If it is Positive, it gets added to the positive sum variable. If it is Negative, it gets added to the negative sum variable; else, it is zero. Then we calculate the Total sum by adding the Array elements with the Total. Finally, we calculate the Average by dividing the total by the number of elements in the array and display the result separately.


STEP 1: Include the Header files into the C program to use the built-in functions.

STEP 2: Declare and Initialize the variables and the Array for the C program.

STEP 3: Accept the number of terms needed from the user using printf and scanf functions in C.

STEP 4: Open 'for loop' to accept numbers and add that to Array using the Array index.

STEP 5: Display the elements in the Array using another 'for loop' in the C program.

STEP 6: Open a for loop from '0' to the 'number of terms' to check each element in the Array.

STEP 7: Using an 'if' condition check if the array element is less than zero, add that to Negative Sum.

STEP 8: Else if the number is greater than zero, add that to the Positive Sum.

STEP 9: Else add the element is zero and nothing to be done.

STEP 10: Add the Array element with the Total Sum in each iteration of the 'for loop'.

STEP 11: Calculate the 'Average' by dividing the Total Sum by the total number of elements in the Array.

STEP 12: print the Positive Sum, Negative Sum, Average using printf in C programming language.

C Source Code

                                          #include <stdio.h>
#define MAXSIZE 10               /* defines array size 10 */

void main() 
   int array[MAXSIZE];
   int i, N, negsum = 0, posum = 0;
   float total = 0.0, averg;
   printf("Enter the value of N\n");
   scanf("%d", & N);
   printf("Enter %d numbers (-ve, +ve and zero)\n", N);            /* enter the user input into the array we defined */
   for (i = 0; i < N; i++) 
      scanf("%d", & array[I]);
   printf("Input array elements\n");
   for (i = 0; i < N; i++) 
      printf("%+3d\n", array[I]);            /* prints the values inside of the array using 3 positions*/

  /* Summing  begins */

  for (i = 0; i < N; i++) 
      if (array[i] < 0) 
         negsum = negsum + array[I];                 /* if number is negative it gets added to negative sum */
     else if (array[i] > 0) 
        posum = posum + array[I];                      /* if number is positive it gets added to positive sum */
    else if (array[i] == 0) 
     total = total + array[I];
   averg = total / N;                  /* calculating average */
   printf("\nSum of all negative numbers    = %d\n", negsum);           /* displays the output */
   printf("Sum of all positive numbers    = %d\n", posum);
   printf("\nAverage of all input numbers   = %.2f\n", averg);


Enter the value of N

Enter 5 numbers (-ve, +ve and zero)

Input array elements
Sum of all negative numbers    = -10

Sum of all positive numbers    = 11

Average of all input numbers   = 0.20