Programming for Problem Solving

GTU Practical 35

35. Write a C program to calculate the average, geometric and harmonic mean of n elements in an array.
#include<stdio.h>  ( this applies in the given code )

#include
#include

int main() {
int n;
float sum = 0, sum1 = 0, prod = 1, a;

printf(“Enter number of elements: “);
scanf(“%d”, &n);

for (int i = 0; i < n; i++) { printf("Enter value %d: ", i + 1); scanf("%f", &a); sum += a; sum1 += 1 / a; prod *= a; } printf("Average = %.2f\nGeometric Mean = %.2f\nHarmonic Mean = %.2f\n", sum / n, pow(prod, 1.0 / n), n / sum1); return 0; }

OUTPUT

Enter number of elements: 2
Enter value 1: 5
Enter value 2: 6
Average = 5.50
Geometric Mean = 5.48
Harmonic Mean = 5.45

GTU STUDY