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

What is an array in C?

This program is to find the sum of all negative numbers and the sum of all positive numbers. Also, find the average of the negative and positive numbers. Here, we are using the concept of arrays 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 using the sequential memory locations, so the lowest 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. 

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 each element is positive, negative, or zero. If it is positive, it gets added to the positive sum. If it is negative, it gets added to the negative sum; 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 Zero 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 Total Sum, Average, Positive Sum, Negative Sum 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;
  printf("\nSum of all negative numbers    = %d\n", negsum); /* calculating average */
  printf("Sum of all positive numbers    = %d\n", posum);
  printf("\nAverage of all input numbers   = %.2f\n", averg); /* displays the output */
} /*End of main()*/


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