Java Algorithm
Java Algorithm
6. Reset amount to the change left after giving out that many dimes.
7. Set the variable nickels = maximum number of nickels possible in amount.
8. Reset amount to the change left after giving out that many nickels.
9. pennies = amount;
10. Output originalAmount and the numbers of each coin.
***********************
Line 2 sets the value of originalAmount it is already Java code, so no need to translate.
Thus far, the main part of the program looks like this:
public static void main(String[] args)
{
// Declare the variables first
int amount, originalAmount, quarters, dimes, nickels, pennies;
System.out.println(Enter a whole number from 1 to 99.);
System.out.println(I will output a combination of coins );
System.out.println(that equals the change you requested.);
Scanner keyboard = new Scanner(System.in);
amount = keyboard.nextInt();
originalAmount = amount;
***********************
Line 3: Set the variable quarters = maximum number of quarters possible in amount.
Line 4: Reset amount to the change left after giving out that many quarters.
Let us understand this with an example:
for 87 cents, you can have a maximum of 3 quarters, because 3 x 25 = 75
remainder amount is 87 75 = 12
But in the actual code, reverse the left and right-hand sides:
quarters = amount/25;
amount = amount%25;
***********************
Lines 5-8:
You realize that dimes and nickels are treated in a similar way, so:
dimes = amount/10;
amount = amount%10;
nickels = amount/5;
amount = amount%5;
***********************
Line 9:
pennies = amount;