fastread homefastrread library fastread menu

C : Calculate Average Using Arrays

Tutorial by:Manisha Dubey      Date: 2016-06-17 03:59:28

❰ Previous Next ❱

This program takes n number of element from user(where, n is specified by user), stores data in an array and calculates the average of those numbers.

Source Code to Calculate Average Using Arrays


#include <stdio.h>
int main(){
    int n, i;
    float num[100], sum=0.0, average;
    printf("Enter the numbers of data: ");
    scanf("%d",&n);
    while (n>100 || n<=0)
    {
        printf("Error! number should in range of (1 to 100).\n");
        printf("Enter the number again: ");
        scanf("%d",&n);
    }
   for(i=0; i<n; ++i)
   {
      printf("%d. Enter number: ",i+1);
      scanf("%f",&num[i]);
      sum+=num[i];
   }
   average=sum/n;
   printf("Average = %.2f",average);
   return 0;
}

Output


Enter the numbers of data: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

 

This program calculates the average if the number of data are from 1 to 100. If user enters value of n above 100 or below 100 then, while loop is executed which asks user to enter value of n until it is between 1 and 100.

❰ Previous Next ❱


C

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website