C Program to calculate Average of numbers entered by user using array.

// Program to find the average of n numbers using arrays
Finding Average using Array

#include <stdio.h>
#include <conio.h>

void main() 
{
  int marks[10], i, n, sum = 0;
  double average;
  printf("Enter number of elements: ");
  scanf("%d", &n);

  for(i=0; i < n; ++i) {
    printf("Enter number%d: ",i+1);
    scanf("%d", &marks[i]);
          
    // adding integers entered by the user to the sum variable
    sum += marks[i];
  }

  // explicitly convert sum to double
  // then calculate average
  average = (double) sum / n;

  printf("Average = %.2lf", average);
getch();
}
Expected output:
Enter number of elements: 5
11
22
33
44
55
Average =33.00

 

Post a Comment

Previous Post Next Post