# Thread: Problem with creating a program that calculates money

1. Member Join Date
Mar 2013
Posts
21
Rep Power
0

## Problem with creating a program that calculates money

My program is supposed to ask the user to enter a dollar value from up to 100 dollars, and output the amount of bills and coins required to create that amount with the least change possible. I'm not completely finished, however when I test run the program and I enter values in the 60s and 90s, it doesnt seem to give me any info past the 50 dollar bill. Why is this happening? Thanks
XML Code:
```import java.io.*;

public class ChangeProgram
{
public static void main (String [] args) throws IOException
{

String inputA;
double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0;

System.out.println("Please enter a dollar amount: ");
input = Double.parseDouble(inputA);

if (input <= 100 && input >= 50){
double answer = input / 50;

System.out.println(fifty + " \$50 bill(s)");
remainder = input - 50;

if(remainder >= 20 && remainder < 50){
double answerB = remainder / 20;

System.out.println(remainderA + " \$20 bill(s)");

double twenty = remainderA * 20;
remainderB = remainder - twenty;

if(remainderB >= 10 && remainderB < 20){
double answerC = remainderB / 10;

System.out.println(remainderC + " \$10 bill(s)");

double ten = remainderC * 10;
remainderD = remainderB - ten;

if(remainderD >= 5 && remainderD < 10){
double answerD = remainderD / 5;

System.out.println(remainderE + " \$5 bill(s)");

double five = remainderE * 5;
remainderF = remainderD - five;

if(remainderF >= 2 && remainderF < 5){
double answerE = remainderF / 2;

System.out.println(remainderG + " toonie(s)");

double two = remainderG * 2;
remainderH = remainderF - two;

if(remainderH >= 1 && remainderH < 2){
double answerF = remainderH / 1;

System.out.println(remainderI + " loonie(s)");

double one = remainderI * 1;
remainderJ = remainderH - one;
System.out.println(remainderJ);

if(remainderJ >= 1/4 && remainderJ < 1){
double answerG = remainderJ / 1/4;

System.out.println(remainderK + " quarter(s)");

double quarter = remainderK * 1/4;
remainderL = remainderJ - quarter;
}
}
}
}
}
}

}

}

}```  Reply With Quote

2. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Problem with creating a program that calculates money

All of your if statements are nested. That won't work. What happens if you enter 20? The first test for 50 fails so the test for 20 never gets executed.

Regards,
Jim  Reply With Quote

3. Member Join Date
Mar 2013
Posts
21
Rep Power
0

## Re: Problem with creating a program that calculates money

Hi Jim,
I removed the nested loops, however, it is still skipping over certain statements. Why is this? Thanks

XML Code:
```import java.io.*;

public class ChangeProgram
{
public static void main (String [] args) throws IOException
{

String inputA;
double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0;

System.out.println("Please enter a dollar amount: ");
input = Double.parseDouble(inputA);

if (input >= 50 && input < 100){
double answer = input / 50;

System.out.println(fifty + " \$50 bill(s)");
remainder = input - 50;
}

if(remainder >= 20 && remainder < 50){
double answerB = remainder / 20;

System.out.println(remainderA + " \$20 bill(s)");

double twenty = remainderA * 20;
remainderB = remainder - twenty;
}

if(remainderB >= 10 && remainderB < 20){
double answerC = remainderB / 10;

System.out.println(remainderC + " \$10 bill(s)");

double ten = remainderC * 10;
remainderD = remainderB - ten;
}

if(remainderD >= 5 && remainderD < 10){
double answerD = remainderD / 5;

System.out.println(remainderE + " \$5 bill(s)");

double five = remainderE * 5;
remainderF = remainderD - five;
}

if(remainderF >= 2 && remainderF < 5){
double answerE = remainderF / 2;

System.out.println(remainderG + " toonie(s)");

double two = remainderG * 2;
remainderH = remainderF - two;
}

if(remainderH >= 1 && remainderH < 2){
double answerF = remainderH / 1;

System.out.println(remainderI + " loonie(s)");

double one = remainderI * 1;
remainderJ = remainderH - one;
System.out.println(remainderJ);
}

}

}```  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Problem with creating a program that calculates money

Look at this section of code.

Java Code:
```if (remainder >= 20 && remainder < 50) {
double answerB = remainder / 20;

System.out.println(remainderA + " \$20 bill(s)");

double twenty = remainderA * 20;
remainderB = remainder - twenty;
}

if (remainderB >= 10 && remainderB < 20) {
double answerC = remainderB / 10;

System.out.println(remainderC + " \$10 bill(s)");

double ten = remainderC * 10;
remainderD = remainderB - ten;
}```
Say you enter 76 as a value.
You already computed at least one \$50.
It gets to the \$20 and figure it needs one \$20. So you have \$6 left. That means the test for \$10 to \$19 will fail. But that is where you set the remainderD. Now when you check for \$5 to \$9, it will fail because remainderD is 0.

Regards,
Jim  Reply With Quote

5. Member Join Date
Mar 2013
Posts
21
Rep Power
0

## Re: Problem with creating a program that calculates money

I understand what your saying, I played around with the code a bit and got it to work with numbers 50 and above but not below. Does it have something to do with th eif statements?
XML Code:
```import java.io.*;

public class ChangeProgram
{
public static void main (String [] args) throws IOException
{

String inputA;
double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0, remainderN = 0, remainderP = 0, remainderR = 0;

System.out.println("Please enter a dollar amount: ");
input = Double.parseDouble(inputA);

if (input >= 50 || input < 100){
double answer = input / 50;

System.out.println(fifty + " \$50 bill(s)");
remainder = input - 50;
}

if(remainder >= 20 || remainder < 50){
double answerB = remainder / 20;

System.out.println(remainderA + " \$20 bill(s)");

double twenty = remainderA * 20;
remainderB = remainder - twenty;
}

if(remainderB >= 10 || remainderB < 20){
double answerC = remainderB / 10;

System.out.println(remainderC + " \$10 bill(s)");

double ten = remainderC * 10;
remainderD = remainderB - ten;
}

if(remainderD >= 5 || remainderD < 10){
double answerD = remainderD / 5;

System.out.println(remainderE + " \$5 bill(s)");

double five = remainderE * 5;
remainderF = remainderD - five;
}

if(remainderF >= 2 || remainderF < 5){
double answerE = remainderF / 2;

System.out.println(remainderG + " toonie(s)");

double two = remainderG * 2;
remainderH = remainderF - two;
}

if(remainderH >= 1 || remainderH < 2){
double answerF = remainderH / 1;

System.out.println(remainderI + " loonie(s)");

double one = remainderI * 1;
remainderJ = remainderH - one;

}

if (remainderJ >= 0.25 || remainderJ < 1){
double answerG = remainderJ / 0.25;

System.out.println(remainderK + " quarter(s)");

double quarter = remainderK * 0.25;
remainderL = remainderJ - quarter;

}

if(remainderL >= 0.10 || remainderL < 0.25){
double answerH = remainderL / 0.10;

System.out.println(remainderM + " dime(s)");

double dime = remainderM * 0.10;
remainderN = remainderL - dime;
}

if(remainderN >= 0.05 || remainderN < 0.10){
double answerI = remainderN / 0.10;

System.out.println(remainderO + " nickel(s)");

double nickel = remainderO * 0.10;
remainderP = remainderN - nickel;
}

if (remainderP >= 0.01 || remainderP < 0.05){
double answerJ = remainderP / 0.01;

System.out.println(remainderQ + " penny(ies)");

double penny = remainderQ * 0.01;
remainderR = remainderP - penny;
}

}

}```  Reply With Quote

6. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Problem with creating a program that calculates money

You don't need separate remainders for you values. Just figure out how many of the largest denomination you can use. Then, using what is left over, do the same for the next highest denomination. When you have a count of a certain denomination which is zero, ignore it.

Regards,
Jim  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•