Index « Previous Next »

Question

Write a program that stores integers given by users in an one dimensional array. Your program should display the sum and average of array.

Source Code

#include <stdio.h>

int main()
{
    int Arr[100], n, i, sum = 0;

    printf("Enter the number of elements you want to insert : ");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        printf("Enter element %d : ", i + 1);
        scanf("%d", &Arr[i]);
        sum += Arr[i];
    }

    printf("\nThe sum of the array is : %d", sum);
    printf("\nThe average of the array is : %0.2f", (float)sum / n);

    return 0;
}

Output

Enter the number of elements you want to insert : 6
Enter element 1 : 10
Enter element 2 : 20
Enter element 3 : 15
Enter element 4 : 18
Enter element 5 : 21
Enter element 6 : 19

The sum of the array is : 103
The average of the array is : 17.17