C Program to Find Sum and Average of an Array

In this tutorial, i am going to show you how to find or calculate sum and average of an array in c program.

Algorithm to Find Sum and Average of an Array

Follow the below given algorithm to write a program to find sum and average of an array of numbers; as follows:

  • Start Program
  • Define array, avg, sum, i and n variables.
  • Take input size of array and store into to the variable.
  • Iterate for loop to take array elements as input, and print them.
  • Iterate for loop to access each element of array to get the sum of all the elements.
  • The average is calculated by dividing the overall sum to the number of elements in the array.
  • Sum and average are printed on the screen.
  • End Program.

C Program to Find Sum and Average of an Array

#include<stdio.h>
int main()
{
 float a[100], sum=0, avg;
 int i, n;
 
 printf("Plesae Enter Size of An Array : ");
 scanf("%d", &n);
 
 /* Reading array */
 printf("Enter array elements or numbers:\n");
 for(i=0; i< n; i++)
 {
  printf("Enter element 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;
}

The result of the above c program; as follows:

Plesae Enter Size of An Array : 4
Enter array elements or numbers:
Enter element a[0] = 10
Enter element a[1] = 20
Enter element a[2] = 30
Enter element a[3] = 40
Sum is 100.000000
Average is 25.000000

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.