# Developers Planet

Ash February 2016

### Round up to nickel, dime, quarters

Here is what I have to do:

Write a program that makes change for amounts less than one dollar. Input to the program must be a positive integer less than 100, representing an amount of money, in cents. Output must be the original amount of money together with a set of coins (quarters, dimes, nickels) that could make up the amount. The program must produce change containing the minimum number of coins required to produce the given amount. Should not include any pennies. For example, the input of 54 should produce results such as:

54 cents requires 2 quarters, 1 nickel

rather than

54 cents requires 2 quarters, 0 dimes, 1 nickels

The following is what i have done so far but doesn't round up if I enter 13,14,23,24,33,34,ect.

Any help would be much appreciated.

``````import java.util.Scanner;

public class MakeChangetest {

public static String makeChange(int change) throws BadChangeException {
int amount;
String x = "";
if (change > 0 && change < 100) {
amount = (int) ((Math.round(change / 5)) * 5);
if (amount == 0 || amount == 100) {
x = "No change to be given.";
}
if (amount == 5) {
x = change + " cents requires " + " 1 nickel ";
} else if (amount == 10 || amount == 20) {
x = change + " cents requires " + (amount / 10) + " dimes ";
} else if (amount == 15) {
x = change + " cents requires " + (amount / 10) + " dime, "
+ " 1 nickel ";
} else if (amount == 25 || amount == 50 || amount == 75) {
x = change + " cents requires " + (amount / 25) + " quaters ";
} else if (amount == 30 || amount == 55 || amount == 80) {
x = change + " cents requires " + (amount / 25) + " quaters, "
+ " 1 nickel";
} else if (amoun        ``````
``` ```
``` ```
``` Answers pseudoDust February 2016 When you write change / 5 both change and 5 are integers, and so Java preforms integer division. Integer division rounds down, so for example: (Math.round(14 / 5)) * 5 => (Math.round(2)) * 5 => 10 To solve this you need one of the operands to be a floating point number: amount = (int) ((Math.round(change / 5.0)) * 5); or amount = (int) ((Math.round(change / 5.0f)) * 5); or amount = (int) ((Math.round((float)change / 5)) * 5); seahorsepip February 2016 You got so many if statements, use a switch instead. I'll update this answer in a sec with a bit cleaned up code. Here's some cleaned up code with the included fix from the other answer: public class MakeChangetest { public static String makeChange(int change) throws BadChangeException { int amount; String x = ""; if (0 <= change <= 100) { amount = (int) ((Math.round(change / 5.0)) * 5); switch(amount) { case 0: case 100: x = "No change to be given."; break; case 5: x = change + " cents requires " + " 1 nickel "; break; case 10: case 20: x = change + " cents requires " + (amount / 10) + " dimes "; break; case 15: x = change + " cents requires " + (amount / 10) + " dime, " + " 1 nickel "; break; case 25: case 50: case 75: x = change + " cents requires " + (amount / 25) + " quaters "; break; case 30: case 55: case 80: x = change + " cents requires " + (amount / 25) + " quaters, " + " 1 nickel"; brea; case 35: case 60: case 70: case 45: case 85: case 95: if (amount % 25 == 10) { x = change + " cents requires " + (amount / 25) + " quater, " + "1 dime "; } else { x = change + "cents requires " + (amount / 25) + " quaters, " + " 2 dimes "; } break; case 40: case 65: case 90: i ```
``` Post Status Asked in February 2016Viewed 1,662 timesVoted 7Answered 2 times Search Leave an answer ```
``` ```
``` ```
``` Quote of the day: live life .btn-primary{ background-color: #f44336 !important; border-color: #f44336 !important; } Devs Planet ® 2014-2016 www.devsplanet.com Devs Planet © all rights reserved Quick Actions Search // Used to toggle the menu on small screens when clicking on the menu button function myFunction() { var x = document.getElementById("navDemo"); if (x.className.indexOf("w3-show") == -1) { x.className += " w3-show"; } else { x.className = x.className.replace(" w3-show", ""); } } ```