# Minimum number of coins to make change

\ \$ \begingroup\ \$

## Printf format

I noticed that at the end of your platform you had 3 cases due to trying to print the count of cents out correctly. You can do it all in 1 case by using the `%02i` format. This format prints an integer with 2 digit width and pads the leading digits with zero .

## Simplifying the logic

right now, your logic for each appellation gets more and more complicate as you go along because you are subtracting more and more things from the entire on each step. If you merely subtracted the amount from the total right field away, you would n’t have to repeat it on each step. For example :

``````int getHundreds = getDollars / 100;
getDollars -= getHundreds * 100;
int getFifties = getDollars / 50;
getDollars -= getFifties * 50;
int getTwenties = getDollars / 20;
getDollars -= getTwenties * 20;
// etc.
``````

## Using functions

now that you have simplified the logic, you can put that logic into a routine. For exercise :

``````/**
* @param        pAmount          Points to amount of money.  Returns the amount
*                                minus the amount used up by this denomination.
* @param        denomination     The value of this denomination.
* @return                        The number of units of this denomination.
*/
int makeChange(int *pAmount, int denomination)
{
int ret = *pAmount / denomination;

*pAmount -= ret * denomination;
return ret;
}

int main(void) {
// ...
int remainingDollars = getDollars;
int getHundreds = makeChange(&remainingDollars, 100);
int getFifties  = makeChange(&remainingDollars,  50);
int getTwenties = makeChange(&remainingDollars,  20);
// etc.
}
``````

## Reducing the number of variables

Since you are n’t actually printing out how many of each bill/coin you are using, you do n’t need indeed many variables. You only actually need to know how many bills plus coins you need. So you could change your code to :

``````    int remainingDollars  = getDollars;
int numBillsPlusCoins = 0;

numBillsPlusCoins += makeChange(&remainingDollars, 100);
numBillsPlusCoins += makeChange(&remainingDollars,  50);
numBillsPlusCoins += makeChange(&remainingDollars,  20);
// etc.
``````

## Using arrays

There ‘s still a draw of repetition. It would be courteous to not have to copy/paste one wrinkle per denomination. You can use arrays to handle each appellation in a loop rather. You just need to create an array with the possible bill denominations and one for the coin denominations .
Some other assortment :

1. I added my own `GetFloat()` function because I don’t have whatever library you are using.
2. I removed the use of `roundf()` and just did the rounding myself.
3. I used a `DIM()` macro which returns the dimensions of a given array. You will encounter this in C a lot for loops which need to loop over a statically defined array.

here is the final code :

``````#include

#define DIM(array)        (sizeof(array)/sizeof(array[0]))

float GetFloat(void)
{
float ret = 0;
scanf("%f", &ret);
return ret;
}

/**
* @param       pAmount         Points to amount of money.  Returns the amount
*                              minus the amount used up by this denomination.
* @param       denomination    The amount for this denomination.
* @return                      The number of units of this denomination.
*/
int makeChange(int *pAmount, int denomination)
{
int ret = *pAmount / denomination;

*pAmount -= ret * denomination;
return ret;
}

int main(void)
{
// declare float
float money;
const int billDenominations[] = { 100, 50, 20, 10, 5, 1 };
const int coinDenominations[] = { 25, 10, 5, 1 };

// Get user input and check if input is positive
do
{
printf("Please input the some amount of money you would like to "
"make change for:  \n");
money = GetFloat();
} while (money < 0);

// Get the Dollars from the Float
int numDollars = (int) money;

// Get the cents from the Float (rounded to nearest cent)
int numCents = (int) ((money - numDollars) * 100 + 0.5f);

int remainingDollars = numDollars;
int remainingCents   = numCents;
int numBillsAndCoins = 0;

// Greedy Algorithm for Dollars
for (int i = 0;i < DIM(billDenominations);i++)
numBillsAndCoins += makeChange(&remainingDollars, billDenominations[i]);

// Greedy Algorithm for Cents
for (int i = 0;i < DIM(coinDenominations);i++)
numBillsAndCoins += makeChange(&remainingCents, coinDenominations[i]);

printf("\nThe least possible steps you can make change from "
"\$%i.%02i is %i. \n", numDollars, numCents, numBillsAndCoins);
}
``````
reference : https://ontopwiki.com
Category : Finance