Hello I'm working on this problem where the user inputs an amount of money between $0.00 and $19.99, and the program outputs the best way to calculate the change of that amount. So for example, if I put in "$9.94", then the program should output
One $5 bill
4 $1 bills
3 quarters
1 dime
1 nickel
4 pennies
float x;
cout << "Input a dollar amount between 0.00 and 19.99 inclusive: ";
cin >> x;
cout << "Change of " << x << " is best given as: " << endl;
if (x > 10.00)
{
cout << "One $10 bill" << endl;
}
elseif (5.00 < x && x <= 9.99)
{
cout << "Two $5 bills" << endl;
}
elseif (0.00 < x && <= 5.00)
{
cout << "Three $5 bills" << endl;
}
but I feel like its really inefficient and am wondering if you guys can point me in the right direction as to how to make this program. I can't think of a way to use loops in this, but if there is a way, will you let me know?
Yes that is very inefficient.. and it will require much more coding than a simpler alternative.
1) Use variables. Make a variable that counts how many of each bill/coin you need.
2) Use division (/) and mod (%) operators to easily find out how many coins you need. Note that mod only works with integers, so I could multiply by 100 to get rid of that floating point.
hint:
This should require exactly zero if statements. You should be able to do it all with math.