I need to break down the amounts on their respective bill/coin.
The output is somewhat like this:
So far, here is my code: (I made the last few codes a comment one ‘cos the
errors come from there)
{
int x,y;
printf("Enter input: ");
scanf("%d",&x);
y=x/1000;
printf("\n# of $1000 bill: %d",y);
x = x%1000;
y=x/500;
printf("\n# of 4500 bill: %d",y);
x = (x%500);
y=x/200;
printf("\n#. of $200 bill: %d",y);
x = (x%200);
y=x/100;
printf("\n# of $100 bill: %d",y);
x = (x%100);
y=x/50;
printf("\n# of $50 bill: %d",y);
x = (x%50);
y=x/20;
printf("\n# of $20 bill: %d",y);
x = (x%20);
y=x/10;
printf("\n#. of $10 coin: %d",y);
x = (x%10);
y=x/5;
printf("\n#. of $5 coin: %d",y);
x = (x%5);
y=x/1;
printf("\n# of $1 coin: %d",y);
x = (x%1);
getch();
return 0;
}
I hope you’ll help me out with this. :/
Thanks!
It’s a simple heuristic problem. You already have the right idea. For a start, if you want to use int (and that’s probably a good idea in your case to avoid fp-precision headaches and the need to use a separate fmod), you’ll have to scale the floating-point input and your modulo/divisors by 100. Also to cut down on some redundancy, consider using a function like:
That should cut down on redundant code a little bit. Maybe not worth getting too fancy for it and I suspect this is homework. Complete solution:
[Edit] If you have trouble understanding pointers, then just do it the way you wrote without using the units function but modify it accordingly to read in a float and multiply by 100 as in the example above.
[Edit] Requested: