Back to 111 Main Page

Mobius strip

Assignment 05


To review the concepts learned so far, write a program that asks the user for a monetary amount and then computes the smallest number of US bills and coins that equal that amount.

Textbook: Chapters 1 and 2 (review)


  • Read in the total amount as a double using a Scanner.
  • Convert this to two ints: the total amount in bills and the total amount in coins. The bills amount can be achieved by simply casting the total amount to an int.
  • The coins amount is a little trickier, since floating point numbers are not always stored exactly and you may get some strange rounding if you just try to take the remainder of dividing by 1 (amount % 1) or otherwise work with values less than 1. Instead, I recommend you compute the whole number of coins by using something like this:
      int coins = (int) Math.rint(amount * 100 % 100);
    Or, once you know the bills amount, you could instead do something like this:
      int coins = (int) Math.rint((amount - bills) * 100);
    In either case, coins would now hold the total number of whole cents (0 to 99), rather than the decimal part of a dollar. (Update: The Math.rint() is needed to handle all cases properly. See FAQ below.)
  • (At this point, temporarily print out the bills and coins values to make sure you've got it right so far. Compile, run, and test your program a few times at this point.)
  • Use a combination of /, %, and additional variables to compute the number of each specific bill and coin needed.


Some sample output (again, user input is shown here in green):

Enter a US dollar amount: 52.67
$52.67 in the fewest number of bills and coins:
2 x $20 bills
1 x $10 bills
0 x $5 bills
2 x $1 bills
2 quarters
1 dimes
1 nickels
2 pennies

Please note the following requirements for your output:

  • Each kind of bill or coin should be listed on its own line. (Each kind of bill or coin should be displayed every time, even if not used to form this particular amount.)
  • Coins should be listed by name: quarter, dime, nickel, penny. (Plural word forms are okay.)
  • Bills can be listed either by name (twenty, ten, five, one), by value ($20, $10, $5, or $1), or by president depicted. (Again, plural form is okay.)
  • Please use only the bills and coins used in the sample output above. That means, no $100 or $50 or $2 bills, and no dollar coins or half-dollars. (Those are too rare in general circulation; cashiers often look at you distrustfully if you try to use them.)

What to Submit

Upload your file to Tamarin.

Grading [5 points]

1 - Compiles
Your program compiles successfully (no errors)
1 - Input
You read in a total decimal amount from the user.
1.5 - Output
You print your output according to the above requirements.
1.5 - Correctness
The given results are correct.


Is there an example of something similar to this?
I'm losing a penny somewhere (or getting other strange rounding errors).
The float and double data types do not always store numbers exactly, especially after a math operation. There are other options available to do more accurate floating-point math, but they are a bit beyond your ability to use at this point. (See float and double on this page in the Java Tutorial for a bit more.)

Instead, you can just round the result to the nearest whole cent. The Math.rint() method takes the double amount you want to round; it returns a double, so you still need to cast the result to an int.

As suggested above, I'd do something like the following:
  int bills = (int) amount;  //number of whole dollars
  int coins = (int) Math.rint((amount - bills) * 100);  //number of cents
Once you've done this, you don't need to worry about rounding or Math.rint() any more, since you're working with bills and coins, which are ints.

However, if you're using double variables throughout, you may need to use Math.rint() more often.

$158.98 is a good test amount to see if your program is working correctly.

~ztomasze Index : TA Details: ICS111: A05
Last Edited: 13 Sep 2008
©2008 by Z. Tomaszewski.