C Program to Find Sum and Average of an Array

C Program to Find Sum and Average of an Array

C program to find sum and average of an array; Through this tutorial, we will learn how to find or calculate sum and average of an array in c program.

Algorithm to Find Sum and Average of an Array

Use the following 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 output 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

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.