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

A personal system is sold at different costs by vendors. To analyze the pricing data, we need to calculate statistical measures like the average cost and range of values.

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

Syntax

Range = Highest Value - Lowest Value
Average = Sum of All Values / Total Count

Solution

The range is the difference between the highest and lowest values in a dataset. The average is the sum of all values divided by the total count. We need to find the maximum and minimum values while calculating the sum.

Example: Calculate Range and Average

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

#include <stdio.h>

int main() {
    int count = 0;
    float value, high, low, sum = 0, average, range;
    
    printf("Enter numbers (enter negative number to stop):<br>");
    
    while (1) {
        scanf("%f", &value);
        
        if (value < 0) {
            break;
        }
        
        count++;
        
        if (count == 1) {
            high = low = value;
        } else {
            if (value > high) {
                high = value;
            }
            if (value < low) {
                low = value;
            }
        }
        
        sum += value;
    }
    
    if (count > 0) {
        average = sum / count;
        range = high - low;
        
        printf("\nStatistical Analysis:<br>");
        printf("Total values: %d<br>", count);
        printf("Highest value: %.2f<br>", high);
        printf("Lowest value: %.2f<br>", low);
        printf("Range: %.2f<br>", range);
        printf("Average: %.2f<br>", average);
    } else {
        printf("No valid data entered.<br>");
    }
    
    return 0;
}

Output

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

Enter numbers (enter negative number to stop):
25.00 30.50 15.00 28.25 58.15 37.00 16.65 42.00 68.45 53.50 -1

Statistical Analysis:
Total values: 10
Highest value: 68.45
Lowest value: 15.00
Range: 53.45
Average: 37.45

Key Points

  • The program uses a while loop instead of goto statements for better structure
  • Range calculation helps understand the spread of pricing data
  • Average provides the central tendency of the cost values
  • Input validation ensures the program handles edge cases properly

Conclusion

This program effectively calculates statistical measures for pricing analysis. The range and average provide valuable insights into the distribution and central value of system costs, helping in decision-making processes.

Updated on: 2026-03-15T14:00:10+05:30

840 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements