algorithmcoin-change

# Algorithm for the least amount of change

Yes I know there has been similar posts to this however after looking through them all I'm still stuck as I'm very new to programming and none of the answers given were specific enough to my problem to help.

Question. Write an efficient ACL (algorithmic computer language) algorithm that, given a cost of an item (less than or equal to one dollar), gives the number of 50 cent, 20 cent, 10 cent, 5 cent and 1 cent coins the buyer would receive if they handed over one dollar. You must minimise the number of coins in the change.

The question is not related to any specific programming language, and the answer can only use simple ACL language like if, if-else, while loops and can't use arrays or other advanced commands.

Here is where I am at:

Algorithm minimum amount of change

``````{
int cost, fifty, twenty, ten, five, one;

fifty = 0;
twenty = 0;
ten = 0;
five = 0;
one = 0;

if (cost <= 50)
{
fifty = 1;

``````

Finished code, thankyou for your help! If you can see any ambiguities or can help me simplify the code please let me know.

``````Algorithm how much change
{
int cost, change, fifty, twenty, ten, five, one;

fifty = 0;
twenty = 0;
ten = 0;
five = 0;
one = 0;

change = 100 - cost;

if (change >= 50)
{
fifty = fifty + 1;
change = change - 50;
}
while (change >= 20)
{
twenty = twenty + 1;
change = change - 20;
}
while (change >= 10)
{
ten = ten + 1;
change = change - 10;
}
while (change >= 5)
{
five = five + 1;
change = change - 5;
}
while (change >= 1)
{
one = one + 1;
change = change - 1;
}

print(one, five, ten, twenty, fifty);
}
``````

Solution

• actually, cost >= 50 need to be checked only once, but this is more generic (for cases over 1 dollar)

``````while (cost >= 50)
{
fifty++;
cost -= 50;
}
while (cost >= 20)
{
twenty++;
cost -=20;
}
...
``````