Home > Back-end >  The output should be 102.80 when I insert the number of 350kWh
The output should be 102.80 when I insert the number of 350kWh

Time:09-28

Please refer to this picture for clear Electric Block usage

The output should be 102.80 when I insert the number of 350kWh but mine output appear RM 180.600006 and the decimal places too long, I have been put / but still appear long decimal. Here the output. The electric consumption will be divided into blocks.

#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

#include <stdio.h>

//For the first 200 kWh (1-200 kWh) per month = RM21.80
//For the next 100 kWh (201-300 kWh) per month = RM33.40
//For the next 300 kWh (301-600 kWh) per month = RM51.60
//For the next 300 kWh (601-900 kWh) per month = RM54.60
//For the next kWh (901 kWh onwards) per month = RM57.10

int main()
{
   float Electric_Consumption, Bill;

   printf("Please insert the Current Electric Consumption: ");

   scanf("%f", &Electric_Consumption);

   if (Electric_Consumption >=1 && Electric_Consumption <= 200)
   {

    Bill = (Electric_Consumption*(21.80/100));
    printf("Your bill need to pay is RM %f", Bill);


   }

   else if(Electric_Consumption >=201 && Electric_Consumption <= 300)
   {

    Bill = (Electric_Consumption*(33.40/100));
    printf("Your bill need to pay is RM %f", Bill);

   }

   else if(Electric_Consumption >=301 && Electric_Consumption <= 600)
   {

   Bill = (Electric_Consumption*(51.60/100));
    printf("Your bill need to pay is RM %f", Bill);

   }

   else if(Electric_Consumption >=601 && Electric_Consumption <= 900)
   {

    Bill = (Electric_Consumption*(54.60/100));
    printf("Your bill need to pay is RM %f", Bill);

   }

   else if(Electric_Consumption >=900)
   {

    Bill = (Electric_Consumption*(57.10/100));
    printf("Your bill need to pay is RM %f", Bill);

   }

   else
   { printf("You inputed wrong electric consumption!");
   }


    return 0;
    }

CodePudding user response:

This

//For the first 200 kWh (1-200 kWh) per month = RM21.80
//For the next 100 kWh (201-300 kWh) per month = RM33.40
//For the next 300 kWh (301-600 kWh) per month = RM51.60
//For the next 300 kWh (601-900 kWh) per month = RM54.60
//For the next kWh (901 kWh onwards) per month = RM57.10

Means, if someone uses 945 kWh, then they need to pay

  • 21.80 for the first 200 kWh, thats 21.8 * 200
  • 33.40 for the next 100 kWh, thats 33.40 * 100
  • 51.60 for the next 300 kWh, thats 51.60 * 300
  • 54.60 for the next 300 kWh, thats 54.60 * 300
  • For the rest it is 57.1, thats 57.10 * 45

Your code used a fixed price for all kWh and only uses the amount to determine this fixed price. Thats wrong.

This can be solved via recursion:

 double calculate_price(double kWh) {
     if (kWh > 900) return (kWh-900)*57.10   calculate_price(kWh-900);
     else if (kWh > 600) return (kWh-600)*54.60   calculate_price(kWh-600);
     else if .... same for the rest....
     else // kWh <= 200 
        return 21.80*kWh;
}
  

CodePudding user response:

The reason your answer is wrong is because the bill price changes for each cumulative kW. In other words, (Electric_Consumption*(33.40/100)) is computing 33.40/100 for every kilowatt instead of the kilowatts between 301 and 600. You'll need to change the formula to add the previous kilowatt costs and then subtract those kilowatts out from Electric_Consumption.

The reason you have too many decimals is because you need to use "%.2f" for your format string.

CodePudding user response:

A solution by pre-calculating the price of the complete slices.

    #include <stdio.h>

//For the first 200 kWh (1-200 kWh) per month = RM 21.80
//For the next 100 kWh (201-300 kWh) per month = RM 33.40
//For the next 300 kWh (301-600 kWh) per month = RM 51.60
//For the next 300 kWh (601-900 kWh) per month = RM 54.60
//For the next kWh (901 kWh onwards) per month = RM 57.10

int main(void)
{
    float Electric_Consumption;
    printf("Please insert the Current Electric Consumption: ");
    scanf("%f", &Electric_Consumption);

    float price_full_slice[] = {21.8*2,
                                21.8*2   33.4,
                                21.8*2   33.4   51.6*3,
                                21.8*2   33.4   51.6*3, 54.6*3};

    float price = 0;
    if(Electric_Consumption>900)
        price = price_full_slice[3]   (Electric_Consumption-900) * 57.1/100;

    if(Electric_Consumption>600 && Electric_Consumption<=900)
        price = price_full_slice[2]   (Electric_Consumption-600) * 54.6/100;

    if(Electric_Consumption>300 && Electric_Consumption<=600)
        price = price_full_slice[1]   (Electric_Consumption-300) * 51.6/100;

    if(Electric_Consumption>200 && Electric_Consumption<=300)
        price = price_full_slice[0]   (Electric_Consumption-200) * 33.4/100;

    if(Electric_Consumption<=200)
        price = Electric_Consumption * 21.8/100;

    printf("%.2f", price);

    return 0;
}
  •  Tags:  
  • c
  • Related