C Program to calculate average of Array
In this c programming example we will learn calculate average of the given array
Algorithm
- Take an array of integers to calculate integers by scanf() function
- Assign values to an array a
- Create for loop to read values from array and add to to variable total
- Finally divide the total value with array size
- It will gives the average of the given numbers
Simple C example to calculate average of numbers
#include <stdio.h>
int main() {
int a[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i) {
scanf("%d", &a[i]);
}
int i,total;
int n = 5;
total = 0;
for(i=0; i<n; i++) {
total += a[i];
}
printf("Average = %f\n", total/(float)n);
return 0;
}
|
Output
Enter 5 integers: 2
4
6
7
9
Average = 5.600000
|