Make Change Algorithm

Problem

Given an amount of money in US currency. Give an algorithm to make change of the given amount using the smallest possible number of coins. Recall that US coins are Dollars (100 cents), Quarters (25 cents), Dimes (10 cents), Nickels (5 cents) and Pennies (1 cent)

Solution

A greedy approach to solve this problem works by making the decision that seems most promising at any moment. We start with an empty set of coins then at every stage without passing the given amount we add the largest to the coins already chosen. For example if the amount is $2.88 then the solution contains 2 dollars, 3 quarters, 1 dime and 3 pennies

Code

Here is the solution in C#

Add a Comment

Your email address will not be published. Required fields are marked *