C Program to Calculate Average Using Arrays

To understand this example, you should have the knowledge of the following C programming topics:


Store Numbers and Calculate Average Using Arrays

Output

Enter the numbers of elements: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

Here, the user is first asked to enter the number of elements. This number is assigned to n.

If the user entered integer is greater less than 1 or greater than 100, the user is asked to enter the number again. This is done using a while loop.

Then, we have iterated a for loop from i = 0 to i . In each iteration of the loop, the user is asked to enter numbers to calculate the average. These numbers are stored in the num[] array.

scanf("%f", &num[i]);

And, the sum of each entered element is computed.

sum += num[i];

Once the for loop is completed, the average is calculated and printed on the screen.