-1

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 %2f 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;
    }
stefaanv
  • 14,072
  • 2
  • 31
  • 53
Tubu Manis
  • 11
  • 1
  • 3
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Sep 27 '22 at 15:42
  • Change the (mentioned) `%2f` to `%.2f`. The first one extends the *whole* field width with padding, if necessary. It does not limit the width. The second one limits the decimal places. For example, `%8.2f` might output `" 180.60"`. – Weather Vane Sep 27 '22 at 15:47
  • the description in comments does not match your code. If consumption is eg 210 then price should be 200* 21.80 + 20* 33.40. If you carry out the steps your code does with pen and paper you will also get the wrong result. Do the reverse: use pen and paper until you get the correct result, then write the code – 463035818_is_not_an_ai Sep 27 '22 at 15:48
  • 1
    Don't post pictures of text. You output is text. Post text as properly formatted text. – Jabberwocky Sep 27 '22 at 15:48
  • in other words: `350 * 51.60/100` is 180.6 and thats what your code computes, but it is wrong – 463035818_is_not_an_ai Sep 27 '22 at 15:50
  • IMHO, you should print the result after the end of the `if-else-if` ladder. No need to make your program longer by repeating code. – Thomas Matthews Sep 27 '22 at 16:37

3 Answers3

1

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.

cwbusacker
  • 507
  • 2
  • 12
1

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;
}
  
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

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;
}
CGi03
  • 452
  • 1
  • 3
  • 8