Question
#include #include int main() { int units = 0; double bill_amount = 0; double oneHundredUnits = 0, oneFiftyUnits = 0, twoFiftyUnits = 0, fiveHundredUnits =
#include
int main() { int units = 0; double bill_amount = 0; double oneHundredUnits = 0, oneFiftyUnits = 0, twoFiftyUnits = 0, fiveHundredUnits = 0; printf("Please enter the amount of units that was consumed: "); scanf("%d", &units);
if(units < 0) printf("Unit consumed cannot be negative. ");
else if(units >= 0 && units <= 100) bill_amount = units*0.2;
else if (units > 100 && units<= 250) { bill_amount += 100*0.2; bill_amount += (units - 100)*0.5; } else if(units > 250 && units <= 500) { bill_amount += 100*0.2; bill_amount += 150*0.5; bill_amount += (units - 250)*0.75; } else if(units>500) { bill_amount += 100*0.2; bill_amount += 150*0.5; bill_amount += 250*0.75; bill_amount += (units-500)*1.0; } else { oneHundredUnits = 100*0.2; oneFiftyUnits = (units-150)*0.5; twoFiftyUnits = (units - 250)*0.75; fiveHundredUnits = (units-500)*1.0; bill_amount:oneHundredUnits+ oneFiftyUnits+ twoFiftyUnits+ fiveHundredUnits; } printf("The bill_amount is $%.2lf. ", bill_amount); printf("Thank you! ");
return 0; }
the calculations seem to be right, but I don't know how to remove the "printf("The bill_amount is $%.2lf. ", bill_amount);" whenever there is a negative. Help!
The instructions are the following:
#include/* In this assignment you will write a program to generate electricity bill, when the program executes it will ask for the consumed unit (int) to input, once that is given by the user (who executes this program), then it will print the bill according to the following rule: Unit Consumed Rate ($ per unit) <= 100 0.2 >100 and <=250 0.5 >250 and <=500 0.75 >500 1.0 Please note that the rates are applied on slabs, that means for unit consumed of 300, first 100 units will be charges @$0.2, then next 150 units @$0.5, and rest of 50 units @0.75 Please completet the code according to the instructions. */ int main(){ //(1) declare an integer variable named units //and initializate 0 to it //(2) declare a double varible named bill_amount //and initializate 0 to it //(3) prompt user to input unit consumed using printf //(4) and then using scanf read the value into //the unit variable. //Now, use if-elseif to decide about rate if (units < 0){ printf("Unit consumed cannot be negative "); } else if(units >= 0 && units <= 100){ // 0 < units <= 100 // for this range 0.2 is the rate bill_amount = units * 0.2; } else if(){ //(5) please complete the code for this block // 100 < units <=250 // please note that the bill must be calculated on slabs } else if(){ //(6) please complete the code for this block // 250 < units <=500 // please note that the bill must be calculated on slabs } else{ //(7) please complete the code for this block // for units > 500 // please note that the bill must be calculated on slabs } //(8) ** finally print the bill amount here, // please note that if the unit consumed is invalid // that is negative then no bill should be printed. ** return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started