In: Computer Science
C Program
1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2.
2. Create a second function readValue to read in an integer value. Put the prompt and scanf statements in the function and remove from main. Call the function three times from main since only one value can be returned at one time pass by value.The function header will look something like this:int readValue(void)STOP! Get this part working before going to part 3.
3. Call a third function to calculate the product of the three integers.Print the product from main.The function header will look something like this:int product (int n1, int n2, int n3)STOP! Get this part working before going to part 4.
4. After the first three functions are working, remove the print statements for average and product from main and create a fourth function called printResults. This function will receive the average and the product and will print them. It will not return a value. The function header will look something like this:void printResults(int prod, float ave)
C PROGRAM
#include <stdio.h>
//called function
float average(int n1,int n2,int n3)
{
float avg = 0;
//calculates the average
avg = n1+n2+n3/3;
// returns the average
return avg ;
}
// called function
int read(void)
{
int num=0;
printf("\nEnter a integer:");
//reads the input
scanf("%d",&num);
return num;
}
// called function
int product(int n1,int n2,int n3)
{
//calculates the product
int pro = n1*n2*n3;
//returns the product
return pro;
}
//called function ,
void printResults(int prod,float ave)
{
//prints the average, product
printf("Average of 3 Integeres:%f\n",ave);
printf("Product of 3 Integeres:%d",prod);
}
//main fucntion
int main()
{
//variables declaration
int n1,n2,n3;
int re1,re2,re3,pro;
float avg = 0;
printf("Enter a 3 intgeres between 1 and 100:\n");
//reads the input from user
scanf("%d",&n1);
scanf("%d",&n2);
scanf("%d",&n3);
//calling a function & passing the parameters & returned
value is stored in avg variable
avg = average(n1,n2,n3);
// calling a function 3 times and returned value is stored in
varaibles
re1 = read();
re2 = read();
re3 = read();
//calling a function, passing the parameter , returned value is
stored in pro variable
pro = product(re1,re2,re3);
//calling a function , passing the parameter
printResults(pro,avg);
return 0;
}
OUTPUT