C Program to calculate average of Array

In this c programming example we will learn calculate average of the given array

Algorithm

  1. Take an array of integers to calculate integers by scanf() function
  2. Assign values to an array a
  3. Create for loop to read values from array and add to to variable total 
  4. Finally divide the total value with array size
  5. 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

Average = 5.600000