This is a problem that wants me to read a double value and determine the fewest number of bills and coin needed to represent the amount.
I'm getting a weird result. I put 5.05
as the moneytotal
, and I'm getting 4
pennies when it should be 1
nickel.
I think its a rounding issue but I can't see how to change it when it the problem states to use double.
Scanner scan=new Scanner(System.in);
double moneytotal, ten, five, one, quarters, dimes, nickels, pennies;
System.out.println("Input number!");
moneytotal=scan.nextDouble();
ten= moneytotal % 10;
System.out.println((moneytotal -ten)/10 + " Ten Dollar
Bills.");
five=ten% 5;
System.out.println(((ten - five)/5) + " Five Dollar Bills.");
one = five % 1;
System.out.println((five - one) + " One Dollar Bills.");
quarters = one % 0.25;
System.out.println(((one - quarters)*4) + " Quarters.");
dimes= quarters % 0.10;
System.out.println(((quarters - dimes)*10) + " Dimes.");
nickels=dimes % 0.05;
System.out.println(((dimes - nickels)*20) + " Nickels.");
pennies=nickels % 0.01;
System.out.println(((nickels - pennies) * 100)+ " Pennies.");
Actual result:
input > 5.05
5 Ten Dollar bills.
0 5 Dollar bills.
0 1 dollar bills.
0 quarters.
0 dimes.
0 nickels.
4 pennies.