I have this calculator that is supposed to return the change when you pay for so
ID: 3734299 • Letter: I
Question
I have this calculator that is supposed to return the change when you pay for something .. the calculator works , except tha it has a bug when i enter number like .90 to .94 cents ,, when enter thise number instead of returning the exact amount is always one penny short. Ex: if it cost .93 cents and i pay with 1 dollar i should be gettirn 1 nickle and 2 pennies as change , but instead i get 1 nickle and 1 penny.
public static int[] Calc(double paid, double price)
{
int change = (int)((paid - price)*100.0);
int dollars=0, quarters=0, dimes=0, nickles=0, pennies=0;
int[] changes = new int[5];
if(change > 0)
{
dollars=change/100;
changes[0]=dollars;
change = change%100;
quarters = change/25;
changes[1]=quarters;
change = change%25;
dimes = change/10;
changes[2]=dimes;
change = change%10;
nickles = change/5;
changes[3]=nickles;
change = change%5;
pennies = change%5;
changes[4]=pennies;
}
return changes;
}
Explanation / Answer
public static int[] Calc(double paid, double price) { int change = (int)(paid*100) - (int)(price*100); int dollars=0, quarters=0, dimes=0, nickles=0, pennies=0; int[] changes = new int[5]; if(change > 0) { dollars=change/100; changes[0]=dollars; change = change%100; quarters = change/25; changes[1]=quarters; change = change%25; dimes = change/10; changes[2]=dimes; change = change%10; nickles = change/5; changes[3]=nickles; change = change%5; pennies = change%5; changes[4]=pennies; } return changes; }