The Minivan Car Rental Company charges $0.25/mile if the total mileage does not
ID: 3841962 • Letter: T
Question
The Minivan Car Rental Company charges $0.25/mile if the total mileage does not exceed 50. If the total mileage is over 50, the company charges $0.25/mile for the first 50 miles, then it charges $0.15/mile for any additional mileage over 50 up to 100 miles. Any additional mile will cost $0.15 minus 29% discount per mile, for any miles over 100 miles (This means that after 100 miles each additional mile is cheaper, each mile is 2% cheaper than the previous mile). Write a program so that if a clerk enters the number of miles, the code would display the cost of each mile and the total owed. Features that must be included Ask user to input the number of miles driven Check to make sure the number of miles driven is an integer!!! If it is not an integer round up to the next mile. Formatted message that outputs the cost/mile for each mile (use fprintf): For example, If l enter 24 miles l expect to get The cost for miles 1-24 was: $0.25/mile 025 For example, If l enter 99 miles expect to get The cost for miles 1-50 was: $0.25/mile 0.15 The cost for miles 50-99 was: $0.15/mile For example, if I enter 105 miles l expect to get 0, 1 The cost for miles 1-50 was: $0.25/mile The cost for miles 51-100 was $0.15/mile The cost for mile 101 was: $0.tt##/mile The cost for mile 102 was: $0.###/mile The cost for mile 103 was: $0.###/mile The cost for mile 104 was: $0.###/mile The cost for mile 105 was: $0.###/mile Nice formatted message displaying the final cost to the user (use fprintf) ANSWER THIS: What is the total cost for 150 miles? (Give me a value)Explanation / Answer
Since, what language is to be used is not given I will use the liberty to write it in C
Here is the code:
#include<stdio.h>
double mile50(int miles){
double total;
if(miles>50){
total=0.25*50;
fprintf(stdout,"The cost for miles 1-%d was: $%lf ",50,total);
}
else if(miles<=50){
total=miles*0.25;
fprintf(stdout,"The cost for miles 1-%d was: $%lf ",miles,total);
}
return total;
}
double mile100(int miles){
double total = mile50(miles);
if(miles>100){
total+=0.15*50;
fprintf(stdout,"The cost for miles 51-%d was: $%lf ",100,0.15*50);
}
else if(miles<=100){
miles = miles-50;
total+=miles*0.15;
fprintf(stdout,"The cost for miles 51-%d was: $%lf ",miles,miles*0.15);
}
return total;
}
double mile(int miles){
double total = mile100(miles);
miles = miles - 100;
int i;
double discount = 0.15;
for(i=1;i<=miles;i++){
discount-=(0.02 * discount);
fprintf(stdout,"The cost for mile %d was: $%lf ",100+i,discount);
total+=discount;
}
return total;
}
int main(){
int miles;
double total=0;
fprintf(stdout,"Enter total miles: ");
scanf("%d",&miles);
if(miles>0 && miles<=50)total=mile50(miles);
else if(miles>50 && miles<=100)total=mile100(miles);
else if(miles>100)total=mile(miles);
if(miles>=0)
fprintf(stdout,"Total owned for %d miles was: $%lf ",miles,total);
else if(miles<0) main();
return 0;
}
And the total cost for 150 miles is: $24.673353 compile and run to see for yourself.
Do comment if you have any doubts !
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.