Suppose that you are working as a cashier in a supermarket and there are
no banknotes left in the cash box. Therefore, for each transaction, you must pay the
remainder of the money by coins.
Write a function numberOfCoins(totalAmount, paidAmount) that returns the min-
imum number of coins required to pay the remainder of the money to the customer.
If the customer pays less than the total amount of transaction, the function should
give the following error message:
The customer paid less than the total amount of the transaction
and abort. Note that you have sucient amounts of 1kr, 5kr, 10kr, 25kr, 50kr, 1TL
coins in the cash box.
The following table shows a possible list of outputs of the function numberOfCoins.
Function call Output
numberOfCoins(99.12, 100) 6
numberOfCoins(17.95, 20) 3
numberOfCoins(10, 10) 0
numberOfCoins(3.99, 3.5) Error
Table 1: A list of possible outputs of the function numberOfCoins
For instance, if the total transaction is 99.12TL and the customer gives you 100TL,
you must pay 1x50kr, 1x25kr, 1×10 kr and 3x1kr coins to the customer (6 coins in
total).
I tried like this but i couldnt:
function coins=numberOfCoins(totalAmount, paidAmount)totalAmount=input('totalAmount: '); paidAmount=input('paidAmount: '); if paidAmount>=totalAmount transaction = paidAmount - totalAmount; while transaction >= 1 transaction = transaction - 1.00; numberOfCoins = numberOfCoins + 1;end if transaction >= 0.50 transaction = transaction - 0.50; numberOfCoins = numberOfCoins + 1; elseif transaction >= 0.25 transaction = transaction - 0.25; numberOfCoins = numberOfCoins + 1; elseif transaction >= 0.10 transaction = transaction - 0.10; numberOfCoins = numberOfCoins + 1; elseif transaction >= 0.05 transaction = transaction - 0.05; numberOfCoins = numberOfCoins + 1 ; elseif transaction >= 0.01 transaction = transaction - 0.01; numberOfCoins = numberOfCoins + 1; end endend
Best Answer