electric.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. // electric.c -- calculates electric bill
  2. #include <stdio.h>
  3. #define RATE1 0.13230 // rate for first 360 kwh
  4. #define RATE2 0.15040 // rate for next 108 kwh
  5. #define RATE3 0.30025 // rate for next 252 kwh
  6. #define RATE4 0.34025 // rate for over 720 kwh
  7. #define BREAK1 360.0 // first breakpoint for rates
  8. #define BREAK2 468.0 // second breakpoint for rates
  9. #define BREAK3 720.0 // third breakpoint for rates
  10. #define BASE1 (RATE1 * BREAK1)
  11. // cost for 360 kwh
  12. #define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1)))
  13. // cost for 468 kwh
  14. #define BASE3 (BASE1 + BASE2 + (RATE3 *(BREAK3 - BREAK2)))
  15. //cost for 720 kwh
  16. int main(void)
  17. {
  18. double kwh; // kilowatt-hours used
  19. double bill; // charges
  20. printf("Please enter the kwh used.\n");
  21. scanf("%lf", &kwh); // %lf for type double
  22. if (kwh <= BREAK1)
  23. bill = RATE1 * kwh;
  24. else if (kwh <= BREAK2) // kwh between 360 and 468
  25. bill = BASE1 + (RATE2 * (kwh - BREAK1));
  26. else if (kwh <= BREAK3) // kwh betweent 468 and 720
  27. bill = BASE2 + (RATE3 * (kwh - BREAK2));
  28. else // kwh above 680
  29. bill = BASE3 + (RATE4 * (kwh - BREAK3));
  30. printf("The charge for %.1f kwh is $%1.2f.\n", kwh, bill);
  31. return 0;
  32. }