C program to calculate range of values and an average cost of a personal system.

CServer Side ProgrammingProgramming

Problem

A personal system is sold at different costs by the vendors.

Let’s take the list of costs (in hundreds) quoted by some vendors −

25.00, 30.50, 15.00, 28.25, 58.15,

37.00, 16.65, 42.00 68.45, 53.50

Solution

Calculate the average cost and range of values.

The difference between the highest and the lowest values in the series is called range Hence, Range = highest value - lowest value.

Now, find the highest and the lowest values in the series.

Example

Following is the C program to calculate the range of values and average cost of a personal system −

 Live Demo

#include<stdio.h>
main(){
   int count;
   float value, high, low, sum, average, range;
   sum = 0;
   count = 0;
   printf("enter no's in line and at end press any negative number\n");
   input:
   scanf("%f", &value);
   if (value < 0)
   goto output;
      count = count + 1;
   if (count == 1)
      high = low = value;
   else if (value > high)
      high = value;
   else if (value < low)
      low = value;
      sum = sum + value;
   goto input;
      output:
   average = sum/count;
   range = high - low;
   printf("\n\n");
   printf("Total values : %d\n", count);
   printf("Highest-value: %f\nLowest-value : %f\n", high, low);
   printf("Range : %f\nAverage : %f\n", range, average);
}

Output

When the above program is executed, it produces the following output −

Enter numbers in line and at end press any negative number
22.4 56.8 12.3 48.6 31.4 19.0 -1
Total values: 6
Highest-value: 56.799999
Lowest-value: 12.300000
Range: 44.500000
Average: 31.750000
raja
Published on 25-Mar-2021 15:54:18
Advertisements