Monday, 3 December 2018

Programming for Problem Solving (3110003) - Program No. 35

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

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

int main()
{
int i,mul=1,a[5];
float sum1=0,sum2=0,havg,gavg,avg;

for(i=0;i<5;i++)

{
printf("enter value of %d element : ",i+1);
scanf("%d",&a[i]);
}

//Calculation of Average
for(i=0;i<5;i++)
{
sum1=sum1+a[i];
}
avg=(float)sum1/5;

//Calculation of Harmonic mean
for(i=0;i<5;i++)
{
sum2=sum2 + (float) 1/a[i];
}
havg=(float)5/sum2;

//Calculation of Geometric mean
for(i=0;i<5;i++)
{
mul=mul*a[i];
}
gavg=pow(mul,(float)1/5);

printf("\n Average of array %f",avg);
printf("\n Harmonic Mean of array %f",havg);
printf("\n Goemetric Mean of array %f",gavg);



}

No comments:

Post a Comment