Create a series of if/elseif statements that check which coin is entered, and se
ID: 3674675 • Letter: C
Question
Create a series of if/elseif statements that check which coin is entered, and sets the variable value to be the correct value of the coin. The following are the cases to consider:
If coin equals ‘q’, then value equals 25
If coin equals ‘d’, then value equals 10
If coin equals ‘n’, then value equals 5
If coin equals ‘p’, then value equals 1
Otherwise, if the coin is none of the above, add a default else statement that sets value to 0
For this part, you are going implement the code that asks the user to enter coins until they have entered enough for the juice.
1. Initialize total to 0. We do this because initially, no coins have been entered.
Using a loop, ask the user to enter a coin until the total matches or exceeds 95 cents. The input should be a char or string, so make sure that you are using the ‘s’ with your input function. Also, be sure that you store the input in a variable named coin
After your loop, print a message to let them know that their juice has been dispensed.
Translate your pseudo-code into working MATLAB code. Be sure that your counter variables are named quarters, dimes, nickels, and pennies
Explanation / Answer
prompt = "Enter a coin";
total = 0;
quarters = 25;
dimes = 10;
nickels = 5;
pennies =1;
while total<95
coin = input(prompt,'s');
if coin == "q"
value = quarters;
elseif coin == "d"
value = dimes;
elseif coin == "n"
value = nickels;
elseif coin == "p"
value = pennies;
else
value = 0;
end
total = total + value;
end
fprintf('You juice is dispensed')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.