Hi to everyone...
I am to write a code about finding the least coins number to make up an amount that is given by user.
It is a trivial question for a programmer and I did code well.
In Canada, there are 2 more currencies, Loonie = 1$ and Twonie = 2$
There is no problem to solve with the currencies.
But the tricky part, there is an extra currency that is not really is called "Jonnie = 1.31$" , that is to make the problem harder.
for example
User enters $4.63. Your output should be:
The least number of coins required to make up $4.64 is 4. The coins needed are:
1 Twonie $2.00
2 Jonnies $2.62
1 Penny $0.01
____________
$4.63
#include <stdio.h>
#include <stdlib.h>
#define FLUSH while (getchar() != '\n')
void makechange(int, int*);
int main(void)
{
int i;
double money;
int numsread = 0;
int change[7];
char *moneytype[7] = {"Twonie(s)\n","Jonnie(s)\n", "Loonie(s)\n", "Quarter(s)\n", "Dimes\n",
"Nickel(s)\n", "Penny(ies)\n"};
while (numsread == 0) {
printf("Please enter the amount of money you would like changed: ");
numsread = scanf("%lf", &money);
FLUSH;
}
money *= 100;
makechange(money, change);
for (i=0; i < 7; i++) {
printf("%d ", change[i]);
printf(moneytype[i]);
}
system("pause");
return 0;
}
void makechange(int moneytotal, int* result)
{
int i;
int moneyvalues[7] = {200, 131, 100, 25, 10, 5, 1};
for (i = 0; i < 7; i++) {
result[i] = moneytotal / moneyvalues[i];
moneytotal -= moneyvalues[i] * result[i];
}
}
Can anyone give me an idea or a way to solution to this question?
thanks