[NumCoins1, UniqueCoinNumber1, CoinsCollection1] = numberOfCoins(99.12, 100)
[NumCoins2, UniqueCoinNumber2, CoinsCollection2] = numberOfCoins(17.95, 20)
[NumCoins3, UniqueCoinNumber3, CoinsCollection3] = numberOfCoins(10, 10)
[NumCoins4, UniqueCoinNumber4, CoinsCollection4] = numberOfCoins(3.99, 3.5)
function [NumCoins, UniqueCoinNumber, CoinsCollection] = numberOfCoins(totalAmount, paidAmount)
coins = [];
if paidAmount<totalAmount
disp('The paid amount is less then the total amount')
NumCoins = NaN;
UniqueCoinNumber = NaN;
CoinsCollection = NaN;
else
transaction = paidAmount - totalAmount;
transaction = uint16(transaction * 100);
while transaction > 0
if transaction >= 100
transaction = transaction - 100;
coins = [coins 1];
elseif transaction >= 50
transaction = transaction - 50;
coins = [coins 0.5];
elseif transaction >= 25
transaction = transaction - 25;
coins = [coins 0.25];
elseif transaction >= 10
transaction = transaction - 10;
coins = [coins 0.1];
elseif transaction >= 5
transaction = transaction - 5;
coins = [coins 0.05];
elseif transaction >= 1
transaction = transaction - 1;
coins = [coins 0.01];
end
end
NumCoins = numel(coins);
UniqueCoinNumber = numel(unique(coins));
CoinsCollection = coins;
end
end
Best Answer