I am writing a program that based on what you enter will determine how much your change will be - it then displays the total of each coin.
The problem is my math - the dimes are not computing right so it gives back more nickles then necessary. Can someone set me straight here. I think other than that it fine.
Here is the code:
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
/**
Computes the amount of, listed by coin type, to be returned from a dollar.
*/
public class Change {
public static void main(String[] args) {
// Make a Scanner to read data from the console
Scanner console = new Scanner(System.in);
// Read the cost of the item
System.out.println("Enter price of item");
System.out.print("(from 25 cents to a dollar, in 5-cent increments): ");
int cost = console.nextInt();
int change = 100 - cost;
int quarters = change / 25;
int remainder = change % 25;
int dimes = remainder / 10;
int nickles = remainder / 5;
System.out.printf("Your item cost %d cents %n", cost);
System.out.printf("Your change is %d cents %n", change);
System.out.println("The coins you will receive are:");
System.out.println("Quarters ");
System.out.println(quarters);
System.out.println("Dimes ");
System.out.println(dimes);
System.out.println("Nickles ");
System.out.println(nickles);
}
}
The problem is my math - the dimes are not computing right so it gives back more nickles then necessary. Can someone set me straight here. I think other than that it fine.
Here is the code:
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
/**
Computes the amount of, listed by coin type, to be returned from a dollar.
*/
public class Change {
public static void main(String[] args) {
// Make a Scanner to read data from the console
Scanner console = new Scanner(System.in);
// Read the cost of the item
System.out.println("Enter price of item");
System.out.print("(from 25 cents to a dollar, in 5-cent increments): ");
int cost = console.nextInt();
int change = 100 - cost;
int quarters = change / 25;
int remainder = change % 25;
int dimes = remainder / 10;
int nickles = remainder / 5;
System.out.printf("Your item cost %d cents %n", cost);
System.out.printf("Your change is %d cents %n", change);
System.out.println("The coins you will receive are:");
System.out.println("Quarters ");
System.out.println(quarters);
System.out.println("Dimes ");
System.out.println(dimes);
System.out.println("Nickles ");
System.out.println(nickles);
}
}