18 Write a program that reads the prices of a shops products

18. Write a program that reads the prices of a shop’s products continuously until the user enters -1. The program should display the lowest price, the highest, and the average of those prices within [5, 30], before it terminates. Assume that the highest price is $100.

Using C please answer the above question. Commenting your code is always appreciated!

Solution

//Please see the code below:

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

int main()
{

   int input=0,min=99999,max=0,count=0;
   double avg=0;
   printf(\"Please Enter the prices of a shop products continuously to exit type -1\ \");

   while(input!=-1)
   {
       scanf(\"%d\",&input);
       if(input==-1)
           break;
       avg+=input;
       count++;
       if(input<min)
           min=input;
       if(input>max)
           max=input;

   }

  
   printf(\"Lowest price is %d , the highest is %d and the average is %f\",min,max,avg/count);
   getch();
   return 0;
}

OUTPUT:

Please Enter the prices of a shop products continuously to exit type -1
10
20
5
4
-1
Lowest price is 4 , the highest is 20 and the average is 9.750000


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site