code in C 2. Write a program to calculate the arithmetic mean, the geometric mea
ID: 3733005 • Letter: C
Question
code in C
2. Write a program to calculate the arithmetic mean, the geometric mean, and the harmonic mean for a set of non-zero numbers. Ask the user to enter values for the numbers between 0 and 10 and enter -99 when they are done. Within the sentinel loop, calculate the required sums and product. Use doubles for the variables. Implement this loop as a do-while loop. arithmetic_mean- t.+ geometric_ mean. harmonic-mean = 1 1 1 Calculate the means after exiting from the loop and print using 3 decimal digits. Test with 8.2, 6.3, 5.2, 4.5, 7.8 3. Write a program that uses a for loop to prompt a user to enter a series of 5 ints, reads in the five ints, and determines which of the five numbers was the largest and which of the five numbers was the smallest. Once the for loop is over, the program should output the largest number and the smallest number to the screen. Test your program with the following numbers: 12 42 96 8 24Explanation / Answer
2.)The program for the given question is given below, the program takes input in the following format
It will calculate the means till we enter -99
the first line of each set takes the number of elements which means are to be calculated
In the second line, we will give the real numbers from 0 to 10
the output will contain the Arithmetic mean, Geometric mean and Harmonic mean in each subsequent line having 3 digits after the decimal
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
while(1)
{
//to exit enter -99 else enter the no. of elements which means are to be calculated
int n,t,i;
float x,p=0,q=1,r=0;
scanf("%d",&n);
if(n==-99)
return 0;
t=n;
do
{
//enter numbers(from 0 to 10)
scanf("%f",&x);
p+=x;
q*=x;
r+=(1.0/x);
t--;
}while(t>0);
printf("%0.3f %0.3f %0.3f ",p/n,pow(q,1.0/n),n/r);
}
return 0;
}
3.)The program for calculating the minimum and maximum from the set of 5 integers is given below, input line takes the five integer values and gives maximum and minimum among them in the subsequent lines
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int i=0,min=10000,max=-10000,x;
for(i=0;i<5;i++)
{
scanf("%d",&x);
if(x>max)
max=x;
if(x<min)
min=x;
}
printf("%d %d",max,min);
return 0;
}
//If you have any doubt regarding the answer please ask in the comment section
//Please upvote
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.