C Program to Find Sum & Average of n Numbers in Array

Question: Write a program in C to read an array containing n numbers and find sum and average of given numbers.


#include<stdio.h>

int main()
{
 float a[100], sum=0, avg;
 int i, n;
 
 printf("Enter n: ");
 scanf("%d", &n);
 
 /* Reading array */
 printf("Enter numbers:\n");
 for(i=0; i< n; i++)
 {
  printf("a[%d] = ", i);
  scanf("%f", &a[i]);
 }
 
 /* Finding sum */
 for(i=0; i< n; i++)
 {
  sum = sum + a[i];
 }
 
 /* Calculating average */
 avg = sum/n;
 
 /* Displaying Result */
 printf("Sum is %f\n", sum);
 printf("Average is %f", avg);
 
 return 0;
}

Output

Enter n: 4↲
Enter numbers:
a[0] = 11.5↲
a[1] = 5.5↲
a[2] = 8.5↲
a[3] = 10.5↲
Sum is 36.000000
Average is 9.000000