A store owner asks you to write a program for use in the checkout process. The p
ID: 3564335 • Letter: A
Question
A store owner asks you to write a program for use in the checkout process.
The program should:
? Prompt the user to enter the cost of the first item.
? Continue to prompt for additional items, until the user enters 0.
? Display the total.
? Prompt for the dollar amount the customer submits as payment.
? Display the change due.
Sample output:
Enter the cost of your first item in dollars, or 0 to quit: 4 cost =
4
Enter the cost of your next item in dollars, or 0 to quit: 39 cost =
39
Enter the cost of your next item in dollars, or 0 to quit: 0
Your total is:
Enter your payment in dollars: Your change is:
$ 43.00 50 $ 7.00
Note the fact that the first column is left justified and the right column is right justified. For formatting, you may assume that the amounts will never be more than $999.99. Therefore, after calculation all monetary values must be printed with 2 decimal places. The output should match the above format exactly (using string formatter we discussed in class).
Explanation / Answer
Answer
clear all;clc;
total=0;
item=1;
count=0;
while(item~=0)
str=sprintf('Enter the cost of the item %d: $',count+1);
item=input(str);
if(item<0)
fprintf('The cost can not be negative. ');
elseif(item>0)
count=count+1;
total=total+item;
end
end
fprintf(' Total amount: $%1.2f',total);
while(total>0)
payment=input(' Enter payment: $');
while(payment<0)
fprintf('The payment amount can not be negative. ');
payment=input('Enter payment: $');
end
change=payment-total;
total=total-payment;
fprintf('Change Due:%1.2f',change);
end
fprintf(' ');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.