Average of Numbers in Array in C Language

To find the average of numbers of an Array, in C programming, find the sum of numbers in this Array and divide this sum with the length of this Array.

C Program

In the following program, we take an array of integers in n, find their sum by iterating over the items of this Array, divide this sum with the number of items in the Array.

We use C For Loop to iterate over the items of Array.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n[] = {1, 4, 2, 3, 8};
    int len = sizeof n / sizeof n[0];
    
    float sum = 0, avg = 0;
    
    for (int i = 0; i < len; i++) {
        sum += n[i];
    }
    
    avg = sum / len;
    printf("Average : %f\n", avg);
    
    return 0;
}

Output

Average : 3.600000
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned how to find the average of numbers in an Array with a C program.