Chapter 4: Selection Problem 4: Gas Bill Write a program that calculates and pri
ID: 3753195 • Letter: C
Question
Chapter 4: Selection Problem 4: Gas Bill Write a program that calculates and prints out bills of the city gas company. The gas rates vary, depending on whether the bill is for residential use, business use, or factory use. A code of r means residential use, a code of b means business use, and a code of f means factory use. Any other code should be treated as an error. The gas rates are computed as follows: Code r: Dh 5.00 plus Dh 0.005 per cubic centimeter . Code b: Dh 1000.00 for the first 4000 cubic centimeter Dh 0.0025 for each additional cubic centimeter . Code f: Dh 1000.00 if usage doesn't exceed 7000 cubic centimeter; Dh 2000.00 if usage is more than 7000 cubic centimeter but doesn't exceed 10000 cubic centimeter Dh 3000.00 if usage exceeds 10000 cubic centimeter Your program should prompt the user to enter the code (type char), and the cubic centimeters of gas used (type int). Your program should print the input data and the amount due. Sample input/output: nter the code and gas consumption: r 3588 he amount due is 22.5 Dh Enter the code and gas consumption: B 5500 The amount due is 1003.75 Dh nter the code and gas consumption: F 12808 he amount due is 3000 Dh nter the code and gas consumption: k 5432 nvalid code!!! Problem 5: if to switch Rewrite the following code for 3 Sx 10 using the switch statement: coutExplanation / Answer
Below is the solution:
code:
#include<stdio.h>
int main()
{
char code; //variable for input code
int gasConsumption; //variable for input
//Input code and gas consumption
printf("Enter the code and gas consumption: ");
scanf("%c",&code);
scanf("%d",&gasConsumption);
//check for the code and calculate the total amount of due
if(code == 'r' || code == 'R'){
float total = (gasConsumption * 0.005) + 5;
printf("The amount due is: %.2f Dh",total);
}
else if(code == 'B' || code == 'b'){
if(gasConsumption>4000){
gasConsumption = gasConsumption - 4000;
float total = (gasConsumption * 0.0025) + 1000;
printf("The amount due is: %.2f Dh",total);
}
else{
float total = 1000;
printf("The amount due is: %.2f Dh",total);
}
}
else if(code == 'F' || code == 'f'){
if(gasConsumption<7000){
float total = 1000;
printf("The amount due is: %.2f Dh",total);
}
else if(gasConsumption > 7000 && gasConsumption < 10000)
{
float total = 2000;
printf("The amount due is: %.2f Dh",total);
}
else if(gasConsumption > 10000)
{
float total = 3000;
printf("The amount due is: %.2f Dh",total);
}
}
else
{
printf("Invalid code!!!");
}
return 0;
}
sample output:
Enter the code and gas consumption: r 3500
The amount due is: 22.50 Dh
Enter the code and gas consumption: B 5500
The amount due is: 1003.75 Dh
Enter the code and gas consumption: F 12000
The amount due is: 3000.00 Dh
Enter the code and gas consumption: K 5432
Invalid code!!!
switch(x>=9 && x<=10)
{
case 1: //for true condition 1 false is 0
cout<<"Exellent ";
break;
}
switch(x == 7 || x == 8)
{
case 1:
cout<<"Satisfactory ";
break;
}
switch(x != 6)
{
case 1:
cout<<"Poor ";
break;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.