You are viewing our Forum Archives. To view or take place in current topics click here.
Stuck with an easy program :-( [Java]
Posted:

Stuck with an easy program :-( [Java]Posted:

Pulsation
  • Christmas!
Status: Offline
Joined: May 25, 201112Year Member
Posts: 427
Reputation Power: 20
Status: Offline
Joined: May 25, 201112Year Member
Posts: 427
Reputation Power: 20
Hello! So i'm trying to do a program where I input a certain amount of cents, and have it output the minimum amount of coins to make up that number. For example,

Enter the change in cents: 212
The minimum number of coins is:
Quarters: 8
Dimes: 1
Nickles: 0
Pennies: 2


That is actually the problem i'm working on now. This is all the code I have so far. I am just starting out in Java so please be nice :-)

The problem with the code is, its not giving me the correct output for dimes. I believe it has something to do with the way i'm dividing the remainder. However, syntax wise, the code is all good and runs.

import java.util.Scanner;

public class Change
{
    public static void main(String[] args)
    {
      int cents;
      int quarters;
      int dimes;
      int nickles;
      int pennies;
      int remainder1;
 
      Scanner change = new Scanner(System.in);
     
      System.out.print("Enter the change in cents: ");
      cents = change.nextInt();
     
      System.out.println("The minimum number of coins is:");
     
      quarters = cents / 25;
      System.out.println("Quarters: " + quarters);
     
      remainder1 = quarters % cents;
      dimes = remainder1 / 10;
      System.out.println("Dimes: " + dimes);
    }
}
#2. Posted:
5FDP_Jekyll
  • TTG Addict
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
The issue is with your method. For one, you do not need an int called remainder1.

Also when you're trying to call the amount of dimes in your code, you're doing this.
quarters / 212 or 8/212 this returns an 8. As 8 is not divisible by 212. So when it goes to the next line of code, you're actually doing an int division of 8/10 which returns zero.

Instead your code for the dimes and subsequent coins should be as followed: (Change the divisor for each coin.

quarters = cents / 25;
         System.out.println("Quarters: " + quarters);
        
         cents = cents % 25;
         dimes = cents / 10;
         System.out.println("Dimes: " + dimes);


So to take this into nickels you would do:

cents = cents % 10;
         nickels = cents / 5;
         System.out.println("Nickles: " + nickles);


I'm sure you could figure out what to do for the pennies.
#3. Posted:
Pulsation
  • 2 Million
Status: Offline
Joined: May 25, 201112Year Member
Posts: 427
Reputation Power: 20
Status: Offline
Joined: May 25, 201112Year Member
Posts: 427
Reputation Power: 20
5FDP_Jekyll wrote The issue is with your method. For one, you do not need an int called remainder1.

Also when you're trying to call the amount of dimes in your code, you're doing this.
quarters / 212 or 8/212 this returns an 8. As 8 is not divisible by 212. So when it goes to the next line of code, you're actually doing an int division of 8/10 which returns zero.

Instead your code for the dimes and subsequent coins should be as followed: (Change the divisor for each coin.

quarters = cents / 25;
         System.out.println("Quarters: " + quarters);
        
         cents = cents % 25;
         dimes = cents / 10;
         System.out.println("Dimes: " + dimes);


So to take this into nickels you would do:

cents = cents % 10;
         nickels = cents / 5;
         System.out.println("Nickles: " + nickles);


I'm sure you could figure out what to do for the pennies.


Got it to work, thank you! Wasn't too bad haha
#4. Posted:
5FDP_Jekyll
  • 2 Million
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Pulsation wrote
5FDP_Jekyll wrote The issue is with your method. For one, you do not need an int called remainder1.

Also when you're trying to call the amount of dimes in your code, you're doing this.
quarters / 212 or 8/212 this returns an 8. As 8 is not divisible by 212. So when it goes to the next line of code, you're actually doing an int division of 8/10 which returns zero.

Instead your code for the dimes and subsequent coins should be as followed: (Change the divisor for each coin.

quarters = cents / 25;
         System.out.println("Quarters: " + quarters);
        
         cents = cents % 25;
         dimes = cents / 10;
         System.out.println("Dimes: " + dimes);


So to take this into nickels you would do:

cents = cents % 10;
         nickels = cents / 5;
         System.out.println("Nickles: " + nickles);


I'm sure you could figure out what to do for the pennies.


Got it to work, thank you! Wasn't too bad haha
You're welcome. We actually did about the same program in the first week of my class. Only real difference was that we also included dollars.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.