% Car Rental Cost Calculation Script
% Function to calculate the cost of renting a car
function calculateCarRentalCost()
% Prompt user for car type
carType = input('Enter the type of car (Sedan/SUV): ', 's');
% Prompt user for the number of days
numDays = input('Enter the number of days rented: ');
% Prompt user for the number of miles driven
numMiles = input('Enter the number of miles driven: ');
% Initialize cost variables
dailyRate = 0;
freeMiles = 0;
costPerMile = 0;
% Determine the daily rate and
free miles based on car type and rental duration
switch lower(carType)
case 'sedan'
if numDays >= 1 && numDays <= 6
dailyRate = 79;
freeMiles = 80;
costPerMile = 0.69;
elseif numDays >= 7 && numDays <= 29
dailyRate = 69;
freeMiles = 100;
costPerMile = 0.59;
elseif numDays >= 30
dailyRate = 59;
freeMiles = 120;
costPerMile = 0.49;
else
fprintf('Invalid number of days.\n'); return;
end
case 'suv'
if numDays >= 1 && numDays <= 6
dailyRate = 84;
freeMiles = 80;
costPerMile = 0.74;
elseif numDays >= 7 && numDays <= 29
dailyRate = 74;
freeMiles = 100;
costPerMile = 0.64;
elseif numDays >= 30
dailyRate = 64;
freeMiles = 120;
costPerMile = 0.54;
else
fprintf('Invalid number of days.\n'); return;
end
otherwise
fprintf('Invalid car type. Please enter Sedan or SUV.\n'); return;
end
% Calculate the total cost
totalCost = numDays * dailyRate;
% Calculate additional miles cost if applicable
if numMiles > freeMiles
additionalMiles = numMiles - freeMiles;
totalCost = totalCost + additionalMiles * costPerMile;
end
% Display the cost rounded to two decimal places
fprintf('The cost of the rent is %.2f $.\n', totalCost
); end
% Run the function three times for different cases
calculateCarRentalCost(); % User input needed for Sedan, 10 days, 769 miles
calculateCarRentalCost(); % User input needed for SUV, 32 days, 4,056 miles
calculateCarRentalCost(); % User input needed for Sedan, 3 days, 511 miles
JSBDYXIgUmVudGFsIENvc3QgQ2FsY3VsYXRpb24gU2NyaXB0CgolIEZ1bmN0aW9uIHRvIGNhbGN1bGF0ZSB0aGUgY29zdCBvZiByZW50aW5nIGEgY2FyCmZ1bmN0aW9uIGNhbGN1bGF0ZUNhclJlbnRh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ICAgICAgICAgIGVsc2VpZiBudW1EYXlzID49IDMwCiAgICAgICAgICAgICAgICBkYWlseVJhdGUgPSA1OTsKICAgICAgICAgICAgICAgIGZyZWVNaWxlcyA9IDEyMDsKICAgICAgICAgICAgICAgIGNvc3RQZXJNaWxlID0gMC40OTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgZnByaW50ZignSW52YWxpZCBudW1iZXIgb2YgZGF5cy5cbicpOwogICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICBlbmQKICAgICAgICBjYXNlICdzdXYnCiAgICAgICAgICAgIGlmIG51bURheXMgPj0gMSAmJiBudW1EYXlzIDw9IDYKICAgICAgICAgICAgICAgIGRhaWx5UmF0ZSA9IDg0OwogICAgICAgICAgICAgICAgZnJlZU1pbGVzID0gODA7CiAgICAgICAgICAgICAgICBjb3N0UGVyTWlsZSA9IDAuNzQ7CiAgICAgICAgICAgIGVsc2VpZiBudW1EYXlzID49IDcgJiYgbnVtRGF5cyA8PSAyOQogICAgICAgICAgICAgICAgZGFpbHlSYXRlID0gNzQ7CiAgICAgICAgICAgICAgICBmcmVlTWlsZXMgPSAxMDA7CiAgICAgICAgICAgICAgICBjb3N0UGVyTWlsZSA9IDAuNjQ7CiAgICAgICAgICAgIGVsc2VpZiBudW1EYXlzID49IDMwCiAgICAgICAgICAgICAgICBkYWlseVJhdGUgPSA2NDsKICAgICAgICAgICAgICAgIGZyZWVNaWxlcyA9IDEyMDsKICAgICAgICAgICAgICAgIGNvc3RQZXJNaWxlID0gMC41NDsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgZnByaW50ZignSW52YWxpZCBudW1iZXIgb2YgZGF5cy5cbicpOwogICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICBlbmQKICAgICAgICBvdGhlcndpc2UKICAgICAgICAgICAgZnByaW50ZignSW52YWxpZCBjYXIgdHlwZS4gUGxlYXNlIGVudGVyIFNlZGFuIG9yIFNVVi5cbicpOwogIC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