PROBLEM 4 Residential and business customers are paying different rates for water usage. Residential customers pay $0.005 per gallon for the first 6000 gallons. If the usage is more than 6000 gallons, the rate will be $0.007 per gallon after the first 6000 gallons. Business customers pay $0.006 per gallon for the first 8000 gallons. If the usage is more than 8000 gallons, the rate will be $0.008 per gallon after the first 8000 gallons. For example, a residential customer who has used 9000 gallons will pay $30 for the first 6000 gallons ($0.005 * 6000), plus $21 for the other 3000 gallons ($0.007 * 3000). The total bill will be $51. A business customer who has used 9000 gallons will pay $48 for the first 8000 gallons ($0.006 * 8000), plus $8 for the other 1000 gallons ($0.008 * 1000). The total bill will be $56. Write a program to do the following. Ask the user which type the customer it is and how many gallons of water have been used. Calculate and display the bill

Respuesta :

Answer:

// here is program in C.

#include <stdio.h>

//main function

int main(void) {

   // variables

   char type;

   int gallon;

   double tot_cost=0;

   printf("Enter the type of customer(B for business or R for residential):");

   // read the type of customer

   scanf("%c",&type);

   // if type is business

   if(type=='b'||type=='B')

   {

       printf("please enter the number of gallons:");

       // read the number of gallons

       scanf("%d",&gallon);

       // if number of gallons are less or equal to 8000

       if(gallon<=8000)

       {

           // calculate cost

          tot_cost =gallon*0.006;

           printf("\ntotal cost is: $ %lf",tot_cost);

       }

       else

       {

           // if number of gallons is greater than 8000

           // calculate cost

           tot_cost=(8000*0.006)+((gallon-8000)*0.008);

           printf("\ntotal cost is: $ %lf",tot_cost);

       }

   }

   // if customer type is residential

   else if(type=='r'||type=='R')

        {

       printf("please enter the number of gallons:");

       // read the number of gallons

       scanf("%d",&gallon);

       // if number of gallons are less or equal to 6000

       if(gallon<=6000)

       {

           // calculate cost

           tot_cost=gallon*0.007;

           printf("\ntotal cost is: $ %lf",tot_cost);

       }

       else

       {// if number of gallons is greater than 6000

       // calculate cost

           tot_cost=(6000*0.005)+((gallon-6000)*0.007);

           printf("\ntotal cost is: $ %lf",tot_cost);

       }

   }

return 0;

}

Explanation:

Ask user to enter the type of customer and assign it to variable "type". If the customer type is business then read the number of gallons from user and assign it to variable "gallon". Then calculate cost of gallons, if   gallons are less or equal to 8000 then multiply it with 0.006.And if gallons are greater than 8000, cost for  first 8000 will be multiply by 0.006 and   for rest gallons multiply with 0.008.Similarly if customer type is residential then for first 6000 gallons cost will be multiply by 0.005 and for rest it will  multiply by 0.007. Then print the cost.

Output:

Enter the type of customer(B for business or R for residential):B

please enter the number of gallons:10000

total cost is: $ 64.000000