Write a C program that calculates the cost of transporting a passenger\'s luggag
ID: 3785855 • Letter: W
Question
Write a C program that calculates the cost of transporting a passenger's luggage according to Table 1 below. The program should let a user enter the class of the passenger and the weight of his or her luggage and then displays the cost. Red is entered by a user Sample Code Execution #1 Enter Passenger Class (E for Economy, B for Business, V for VIP): B Enter luggage weight: 39 Total cost is $5.00 Sample Code Execution #2 Enter Passenger Class (E for Economy, B for Business, V for VIP): E Enter luggage weight: 43.5 Total cost is $29.50 Sample Code Execution #3 Enter Passenger Class (E for Economy, B for Business, V for VIP): D Enter luggage weight: 35 Wrong passenger class Test cases: (Passenger Class, Weight) = (V, 75), (E, 15), (B, 60), (E, 35), (P, 25)Explanation / Answer
#include <stdio.h>
int main()
{
char ch;
double weight;
double cost = 0;
printf("Enter the passenger class (E for Economy, B for Business, V for VIP): ");
scanf("%c", &ch);
printf("Enter the luggage weight: ");
scanf("%lf", &weight);
if(ch == 'E'){
if( weight <= 25){
cost = 0;
}
else if(weight > 25 && weight <=40 ){
cost = 1.5 * (weight - 25);
}
else {
cost = (weight - 40) * 2;
weight = 40;
cost =cost + 1.5 * (weight - 25);
}
printf("Total cost is $%lf ", cost);
}
else if(ch == 'B'){
if( weight <= 35){
cost = 0;
}
else if(weight > 35 && weight <=50 ){
cost = 1.25 * (weight - 35);
}
else {
cost = (weight - 50) * 1.5;
weight = 50;
cost =cost + 1.25 * (weight - 35);
}
printf("Total cost is $%lf ", cost);
}
else if(ch == 'V'){
if( weight <= 60){
cost = 0;
}
else {
cost =30;
}
printf("Total cost is $%lf ", cost);
}
else{
printf("Wrong passenger class ");
}
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter the passenger class (E for Economy, B for Business, V for VIP): B
Enter the luggage weight: 39
Total cost is $5.000000
sh-4.2$ main
Enter the passenger class (E for Economy, B for Business, V for VIP): E
Enter the luggage weight: 43.5
Total cost is $29.500000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.