Question

Write C code to repeatedly ask the user for a number, then once the user enters...

Write C code to repeatedly ask the user for a number, then once the user enters 0, the code displays the min, max and average of all values entered.

To get proper credit, name the variables as listed below and follow the directions carefully. Do not use any break or similar type of statements.

To implement the above code, you will need to count the entries - name the counter variable num_count.

You will also need to use a variable, name it min_in so each time the user gives input, and the input is not 0, then compare min_in with the input, if the min_in is > input, then assign min_in to the input.

Similarly name another variable max_in, and compare it to the input, but assign max_in to the input only if max_in is < input.

To calculate the average, accumulate each input into a variable, total. Then after the loop the average is the total divided by the number of entries.

Once the loop is over, display the calculated values (min, max and the average) with a proper wording in three lines. The input is not restricted to integer value so declare the variables above with a proper type. Show the output using two decimal places, and align the three output values as in this sample output:

The minimum entry was: 2.30
The maximum entry was: 62.51
The average of all entries was: 52.81

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<stdio.h>                                               //header files including
int main()
{
   int num_count=0;                                           //num_count variable for counting the entries
   float input_number,min_in,max_in,total;               //input_number,min_in,max_in,total variables
   printf("Enter a Number : ");      
   scanf("%f",&input_number);                       //taking input from user
   while(input_number!=0)                           //itearte the loop until the input number is equal to zero
       {
       if(num_count==0)                           //if num_count=0 means for first number make that as minimum and maximum and add it to total
           {
           min_in=input_number;               //assign input number as minimum
           max_in=input_number;               //assign input as maximum
           total=total+input_number;           //adding input number to total
           }
       else                                                   //else case
           {  
           if (min_in>input_number)               //checking the input is less than existing minimum value or not
               min_in=input_number;               //if yes store that value into min_in
           if (max_in<input_number)               //checking the input is greater than existing maximum value or not
               max_in=input_number;               //if yes store that value into max_in
           total=total+input_number;               //add input_number to total
           }
       num_count++;                           //input_number count will increase used for average calculation
       printf("Enter a Number : ");           //asking user to enter the number repeatedly
       scanf("%f",&input_number);
       }
   printf("The minimum entry was: %.2f\n",min_in);               //finally printing the minimum value of entries
   printf("The maximum entry was: %.2f\n",max_in);               //maximum value of entries
   printf("The average of all entries was: %.2f\n",(total/num_count));           //average value calculated by total/num_count all are rounded to two decimals
   return 0;
}

Code screenshots and output:

#include<stdio.h> int main() //header files including //num_count variable for counting the entries //input_number, min in,ma

Enter a Number : 2.50 Enter a Number : 9.897 Enter a Number : 17.89 Enter a Number : 62.50 Enter a Number : 98.08 Enter a Num

Note: if you have any queries please post a comment thanks a lot...always available to help you

Add a comment
Know the answer?
Add Answer to:
Write C code to repeatedly ask the user for a number, then once the user enters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT