Write a program to find the weighted average of a collection of numbers

Source Code

#include <stdio.h>

int main()
{
    float value, weight, sum = 0;
    int index = 0;
    char str[10];
    printf("Calculation of weighted average\n");
    do {
        printf("enter value : ");
        scanf ("%f", &value);
        printf("enter weight in fraction : ");
        scanf ("%f", &weight);
        printf("effective value : %f considered.\n", value * weight);
        sum += value * weight;
        index++;
        printf("current sum : %f and counts : %d\n", sum, index);
        printf("Do you want to enter another value? ");
        scanf ("%s", str);
    } while(tolower(str[0]) == 'y');
    sum /= index;
    printf("weighted average = %f\n", sum);

    return 0;
}

Output

Calculation of weighted average
enter value : 1
enter weight in fraction : 1.0
effective value : 1.000000 considered.
current sum : 1.000000 and counts : 1
Do you want to enter another value? yes
enter value : 2
enter weight in fraction : 0.5
effective value : 1.000000 considered.
current sum : 2.000000 and counts : 2
Do you want to enter another value? yes
enter value : 3
enter weight in fraction : 0.5
effective value : 1.500000 considered.
current sum : 3.500000 and counts : 3
Do you want to enter another value? no
weighted average = 1.166667

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code