Search code examples
javacoin-change

How to use int output to calculate other output?


Create a Change application that prompts the user for an amount less than $1.00 and then displays the minimum number of coins necessary to make the change.
The change can be made up of quarters, dimes, nickels, and pennies.

Here is what I did:

public static void main(String[] args) {
    System.out.println("Enter the amount in cents: ");
    int a = in.nextInt();
    if(a <= 100){

    }else{
        System.out.println("Try Again");
        System.exit(0);
    }
    {
        int q = (a/25);
        int d = (a/25);
        System.out.println("Quarters: " + q);
        System.out.println("Dimes: " + d);
    }
}

}

D is irrelevant. because I don't know how to get the remainder of quarters and divide it by 10 and so on all the way through pennies


Solution

  • To find the remainder of anything, subtract off the amount you've taken.

    For free, you need to subtract off the coin price times the amounts as you go.

    if(a <= 100){
        int q = a/25;
        a = a-q*25;
        int d = a/10;
        a = a-d*10;
    }