C Program to Calculate Average of Array Elements

In this program, the user is ask to enter 5 integer elements that will save in array arr. Then we calculate the sum and average of these number display it on console.



Source Code
#include<stdio.h>

int main()
{
    int arr[5], i, sum = 0;
    float avg;
    
    printf("Enter 5 array students\n");
    /* Input marks of 10 students */
    for(i=0; i<5; i++)
        scanf("%d", &arr[i]);

    /* Logic to calculate sum and average*/
    for(i=0; i<5; i++)
    {
        sum = sum + arr[i];
    }
    avg = sum / 5;

    /* Print sum */
    printf("\nSum = %d", sum);
    printf("\nAverage = %0.2f", avg);
    return 0;
}
Output
Enter 5 array students
4 2 5 8 6

Sum = 25
Average = 5.00





"Coding Hub - Learn to code" app now available on Google Play Store