**Exercise: **

Write a C Program to calculate average using array.

**Click Here to View the Solution!**

**#include **<stdio.h>
**int **main() {
**int **num, i;
**float **number[100] ;
**float **sum = 0.0;
**float **avg;
printf(**"Enter the numbers of elements: "**);
scanf(**"%d"**, &num);
**while **(num > 100 || num < 1) {
printf(**"Error! number should in range of (1 to 100).\n"**);
printf(**"Enter the number again: "**);
scanf(**"%d"**, &num);
}
**for **(i = 0; i < num; ++i) {
printf(**"%d. Enter number: "**, i + 1);
scanf(**"%f"**, & number[i]);
sum += number[i];
}
avg = sum /num;
printf(**"Average = %.2f"**, avg);
**return **0;
}

**Click Here to View the Output!**

```
Enter the numbers of elements: 3
1. Enter number: 5
2. Enter number: 7
3. Enter number: 3
Average = 5.00
```

**Click Here to View the Explanation!**

- This program is used to calculate the average of numbers by using Arrays.
- In
`main()`

, the program requests the user to enter the number of elements they want to find the average of and stores it in a variable`num`

using the`scanf() function`

. - A
`while loop`

is initialized with the condition that checks the number entered by the user. If the number is greater than 100 or less than 1, the program requests the user to enter a number again between 1 and 100. - A
`for loop`

is then used which iterates between`i = 0`

and`i < num`

. In each iteration, the program requests the user to enter a number for calculating the average and stores these values in a floating-point`array num[i]`

. Each time a number is stored in`num[i]`

, it is added to the variable`sum`

which will hold the sum of all the numbers. - Finally, the average of the numbers is calculating by dividing the
`sum`

by`num`

and the result is printed.