matlab help please ! Q3. Buy 2 get 1 Free: Your favorite restaurant announced a
ID: 3708659 • Letter: M
Question
matlab help please !
Q3. Buy 2 get 1 Free: Your favorite restaurant announced a promotional offer that says buy 2 meals
you get the 3rd one FREE and you decided to invite your family and friends to take advantage of this offer.
Use while loop to write a code that will determine the number of paid meals and the number of
free meals you can get based on the budget and meal cost the user inputs. The program should
also determine the remaining balance. Use another while loop to repeat the code until the user
choose to quit. After each run prompt the user: Run the code again? Y/N or 1/0
Sample outputs:
For $6 meal, $23 can get you 3 meal(s), 1 FREE meal(s) and you keep $5
Run the code again? 1/0 [default:1]
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
meal_cost = input('Enter the meal cost: ');
budget = input('Enter your budget: ');
numMeals = 0; %number of paid meals
freeMeals = 0; %number of free meals
remaining = budget; %remaining money
mealCount = 0; %counter to keep track for every 2nd meal
while remaining > meal_cost
numMeals = numMeals + 1;
mealCount = mealCount + 1;
if mealCount == 2 %for every second meal , add a free meal
freeMeals = freeMeals + 1;
mealCount = 0; %reset meal count
end
remaining = remaining - meal_cost;
end
fprintf("For $%d meal, $%d can get you %d meal(s), %d FREE meal(s) and you keep $%d ", meal_cost, budget, numMeals, freeMeals, remaining)
output
------
Enter the meal cost: > 6
Enter your budget: > 23
For $6 meal, $23 can get you 3 meal(s), 1 FREE meal(s) and you keep $5
?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.