Problem 6: Write a program that determines the change given back to a customer i
ID: 3715656 • Letter: P
Question
Problem 6: Write a program that determines the change given back to a customer in a self-service checkout machine of a supermarket for purchases of up to S10. The program generates a random number between 0.01 and 10.00 and displays the number as the amount to be paid. The program then asks the user to enter payment, which can be one S1 bill, one $5 b or one S10 bill. If the payment is less than the amount to be paid, an error message is displayed. If the payment is sufficient, the program calculates the change and lists the bills and/or the coins that make up the change, which has to be composed of the least number each of bills and coins. For example, if the amount to be paid is S2.33 and a SI0 bill is entered as payment, then the change is one S5 bill, two SI bills, two quarters, one dime, one nickel, and two pennies. Hand in your MATLAB program and outputExplanation / Answer
Solution:
code:
V = 0.01:50.00;
amount_tobe_paid=V(randi([1,numel(V)]))
prompt = 'Enter Payment $1 $5 $10 $20 $50 ';
payment = input(prompt)
if payment < amount_tobe_paid
disp('Payment amount cant be less than amount to be paid')
else
r=amount_tobe_paid-payment;
Fifties=fix(r/50);
r=rem(r, 50);
Twenties=fix(r/20);
Remainder=rem(r, 20);
Tens=fix(r/10);
r=rem(r, 10);
Fives=fix(r/5);
r=rem(r, 5);
> r=rem(r, 1);
Quarters=fix(r/0.25);
r=rem(r, 0.25);
Dimes=fix(r/0.10);
r=rem(r, 0.10);
Nickels=fix(r/0.05);
r=rem(r, 0.05);
Pennies=r/0.01;
disp(['Change given is: $' num2str(amount_tobe_paid-payment)])
disp(['Given in the form of ' num2str(Fifties) ' fifties, ' num2str(Twenties) ' twenties, ' num2str(Tens) ' tens, ' num2str(Fives) ' fives, ' num2str(Ones) ' ones, ' num2str(Quarters) ' quarters, ' num2str(Dimes) ' dimes, ' num2str(Nickels) ' nickels, and ' num2str(Pennies) ' pennies.'])
end
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)