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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *