Problem 35

author
By Supreme2023-03-15
Problem Statement :
Write a C program to calculate the average, geometric and harmonic mean of n elements in an array.
Code:

#include <stdio.h>
#include <math.h>

int main(){

//35. Write a C program to calculate the average, geometric and harmonic mean of n elements
//in an array.

printf("\tAverage, Geometric And Harmonic Mean");
printf("\n===================================================\n\n");

int i,sum=0,n;
float num[100],avg,hmean,gmean,x,Y=1;


printf("Enter The Number Of Elements : ");
scanf("%d",&n);


for(i=0;i<n;i++){
printf("\nEnter Element [%d] :",i+1);
scanf("%f",&num[i]);

sum += num[i];

x += (1.0 / num[i]);
Y = (float)Y * num[i];
}

avg = (sum/n);
gmean = pow(Y, (1.0 / n));
hmean = n * pow(x, -1);

printf("\n---------------------------------------------------\n");

printf("\nAverage = %.2f",avg);
printf("\n\nGeometric Mean = %.2f", gmean);
printf("\n\nHarmonic Mean = %.2f", hmean);

printf("\n---------------------------------------------------\n\n");

return 0;
}

Description :
  • This code is a C program that calculates the average, geometric, and harmonic means of a list of numbers stored in an array.
  • The program begins by printing out a title and a separator.
  • The program then declares and initializes some variables, including an array to hold the input numbers, and variables to hold the sum, average, geometric mean, and harmonic mean.
  • The user is prompted to enter the number of elements to be processed.
  • A loop is used to read in the input numbers, summing them as they are entered.
  • Within the loop, additional calculations are made to determine the geometric and harmonic means.
  • After all of the input numbers have been read, the program calculates the average, geometric mean, and harmonic mean.
  • Finally, the program prints out the results using a separator.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.