im writing a Coin program that tells the user to input a integer between 1 and 99..and reads back how many quarters, dimes, nickels, and pennies back. this is what i have.

import java.util.Scanner;

public class Main 
{
    public static void main( String[] args )
    {
        Scanner input = new Scanner( System.in );
        int number1;
        int quarters;
        int nickels;
        int dimes;
        int pennies;

        System.out.print( "Enter a number between 1 and 99:");
        number1 = input.nextInt();

        quarters = number1 / 25;
        dimes = number1  / 10;
        nickels = number1 /5;
        pennies = number1 /1;

        System.out.printf( "Number of quarters %d\n", quarters);
        System.out.printf( "Number of dimes %d\n", dimes);
        System.out.printf("Number of nickels %d\n", nickels);
        System.out.printf("Number of pennies %d\n",pennies);

    }
}

can anybody help???

Can you show what the program does now and explain what the problem is?

How would you do it if you did it manually? Would you subtract the amount for the quarters when looking for the dimes?

Can you show what the program does now and explain what the problem is?

How would you do it if you did it manually? Would you subtract the amount for the quarters when looking for the dimes?

Enter a number between 1 and 99:99
Number of quarters 3
Number of dimes 9
Number of nickels 19
Number of pennies 99
..

after the first operation. the output for the quarters is right but the nickels dimes and pennies should resemble the rest of the left over numbers

but the nickels dimes and pennies should resemble the rest of the left over numbers

What does "resemble" mean?

Did you read all of my first post?

my bad...that last reply was is the output of the program. the problem is im trying to get the the remainder of the integer after the first operation with the quarters to print out the remainer of the integer. hopes this helps

problem is im trying to get the the remainder of the integer after the first operation with the quarters

Can you describe the steps you must take to compute that?

The remainder: Is that from division or from when you subtract a value from another value.

Do you mean in total how many of each does it contain or is it more like say, 99p consists of 3 quarters, 2 dimes, 0 nickels and 4 pennies?
That's simple enough you just need to pass the values along to each. Try this:

int quarters = value/25;
		int qRemainder = value - ( quarters * 25 );
		System.out.println( "Quarters: "+quarters); // +"\nRemainder: "+qRemainder+"c" );

The commented section is used to verify whether the remainder to be passed on is correct. You could then use qRemainder as the next value for the dimes to use in much the same way only you divide it by 10 - because a dime is 10p.

Hope this helps

commented: Give help, not code. The poster's not as dumb as you think. +0

Btw if you want the user to stay within the parameters then you need to use an if statement and put the code within it. Btw the output of the above that I used, when 99 has been entered is:

Quarters: 3
Dimes: 2
Nickels: 0
Pennies: 4

thanks for the help guys.. i used the qRemainder and vaule example it works but one more question my nickel var. is still wrong.

import java.util.Scanner;

public class Main 
{
    public static void main( String[] args )
    {
        Scanner input = new Scanner( System.in );
        int number1;
        int qremainder;
        int quarters;
        int dimes;
        int nickels;
        int pennies;

        System.out.print( "Enter a number between 1 and 99:");
        number1 = input.nextInt();

        quarters = number1 / 25;
        qremainder = number1 - ( quarters * 25);
        dimes = qremainder / 10;
        qremainder = number1 - ( dimes * 10);
        nickels = number1 / 5;
        qremainder = number1 - ( nickels * 5); 
        pennies = qremainder / 1;


        System.out.printf( "Number of quarters %d\n", quarters);
        System.out.printf( "Number of dimes %d\n", dimes);
        System.out.printf("Number of nickels %d\n", nickels);
        System.out.printf("Number of pennies %d\n",pennies);

    }
}

and this the output
Enter a number between 1 and 99:99
Number of quarters 3
Number of dimes 2
Number of nickels 19
Number of pennies 4

Try debugging your code by adding println() statements to the code to show the values of ALL the variables that are important at that step in your logic. You should then see why the answer is wrong.

For example after this statement:
qremainder = number1 - ( nickels * 5);
Add this:
System.out.println("qremainder=" + qremainder + ", number1=" + number1 +", nickels=" + nickels);

Member Avatar for coil

That's because you're finding the nickels by dividing number1 by 5, not qremainder. Probably a simple oversight.

When you do this sort of println debugging, you should be testing a hypothesis. Go through the code and try to figure out what each value should be, in your understanding of the code.

Start with your line

number1 = input.nextInt();

(ahem, code tags next time, so I have line numbers, please, thanks)

Suppose you enter some number - 83 cents, or whatever you like.
What happens then? And then, and then, and then? At each stage, try to figure out what the number should be, and what it will be.
Use the println debugging to check your reading of the code. Odds are, though, you'll do a lot less println debugging if you practice reading code this way. It'll help you a lot when you get into more complex code.


int qRemainder = value - ( quarters * 25 );

While you're at it, look up the modulus operator (%) and use it to replace this mess.

You shouldn't use the entered value to determine all the remainders - its only passed to the quarters which then produces its own qremainder which is used by the dimes, which produces its own dremainder which is used by the nickels.....and so on.

Example:

int nickels = dRemainder/5;
		int nRemainder = dRemainder - ( nickels * 5 );

"dremainder" is just the whats left after the dimes have been counted.

When you do this sort of println debugging, you should be testing a hypothesis. Go through the code and try to figure out what each value should be, in your understanding of the code.

Start with your line

number1 = input.nextInt();

(ahem, code tags next time, so I have line numbers, please, thanks)

Suppose you enter some number - 83 cents, or whatever you like.
What happens then? And then, and then, and then? At each stage, try to figure out what the number should be, and what it will be.
Use the println debugging to check your reading of the code. Odds are, though, you'll do a lot less println debugging if you practice reading code this way. It'll help you a lot when you get into more complex code.


While you're at it, look up the modulus operator (%) and use it to replace this mess.

Sorry but what do you mean this mess? The code works fine - i entered in 83 and got this output:

Quarters: 3
Dimes: 0
Nickels: 1
Pennies: 3

Which is correct - so i hardly think it can be considered mess just because there are other ways to do it.
The best ways should be the simplest in my opinion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.