write a function that takes a total value of change and returns the correct number of dollars, dimes, nickels, and pennies using as many of the higher valued coins in matlab

main.m
function [dollars, dimes, nickels, pennies] = calculateChange(total)
    dollarValue = 100;
    dimeValue = 10;
    nickelValue = 5;

    % Calculate the number of dollars
    dollars = floor(total / dollarValue);
    remainingAmount = total - dollars * dollarValue;

    % Calculate the number of dimes
    dimes = floor(remainingAmount / dimeValue);
    remainingAmount = remainingAmount - dimes * dimeValue;
    
    % Calculate the number of nickels
    nickels = floor(remainingAmount / nickelValue);
    remainingAmount = remainingAmount - nickels * nickelValue;
    
    % Pennies will be what's left after using dollars, dimes, and nickels
    pennies = remainingAmount;

end
684 chars
22 lines

related categories

gistlibby LogSnag