Problem with creating a program that calculates money

• 09-04-2013, 11:29 PM
incxx
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
Code:

```import java.io.*; public class ChangeProgram {   public static void main (String [] args) throws IOException   {     BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));         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: ");     inputA = myInput.readLine();     input = Double.parseDouble(inputA);         if (input <= 100 && input >= 50){       double answer = input / 50;       double fifty = Math.floor(answer);             System.out.println(fifty + " \$50 bill(s)");       remainder = input - 50;             if(remainder >= 20 && remainder < 50){         double answerB = remainder / 20;         double remainderA = Math.floor(answerB);                 System.out.println(remainderA + " \$20 bill(s)");                 double twenty = remainderA * 20;         remainderB = remainder - twenty;                   if(remainderB >= 10 && remainderB < 20){         double answerC = remainderB / 10;         double remainderC = Math.floor(answerC);                 System.out.println(remainderC + " \$10 bill(s)");                 double ten = remainderC * 10;         remainderD = remainderB - ten;             if(remainderD >= 5 && remainderD < 10){         double answerD = remainderD / 5;         double remainderE = Math.floor(answerD);                 System.out.println(remainderE + " \$5 bill(s)");                 double five = remainderE * 5;         remainderF = remainderD - five;             if(remainderF >= 2 && remainderF < 5){         double answerE = remainderF / 2;         double remainderG = Math.floor(answerE);                 System.out.println(remainderG + " toonie(s)");                 double two = remainderG * 2;         remainderH = remainderF - two;             if(remainderH >= 1 && remainderH < 2){         double answerF = remainderH / 1;         double remainderI = Math.floor(answerF);                 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;         double remainderK = Math.floor(answerG);                 System.out.println(remainderK + " quarter(s)");                 double quarter = remainderK * 1/4;         remainderL = remainderJ - quarter;       }       }     }   }   }  }       }     }   }```
• 09-04-2013, 11:43 PM
jim829
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
• 09-04-2013, 11:58 PM
incxx
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

Code:

``` import java.io.*; public class ChangeProgram {   public static void main (String [] args) throws IOException   {     BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));         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: ");     inputA = myInput.readLine();     input = Double.parseDouble(inputA);         if (input >= 50 && input < 100){       double answer = input / 50;       double fifty = Math.floor(answer);             System.out.println(fifty + " \$50 bill(s)");       remainder = input - 50;     }             if(remainder >= 20 && remainder < 50){         double answerB = remainder / 20;         double remainderA = Math.floor(answerB);                 System.out.println(remainderA + " \$20 bill(s)");                 double twenty = remainderA * 20;         remainderB = remainder - twenty;       }                   if(remainderB >= 10 && remainderB < 20){         double answerC = remainderB / 10;         double remainderC = Math.floor(answerC);                 System.out.println(remainderC + " \$10 bill(s)");                 double ten = remainderC * 10;         remainderD = remainderB - ten;       }               if(remainderD >= 5 && remainderD < 10){         double answerD = remainderD / 5;         double remainderE = Math.floor(answerD);                 System.out.println(remainderE + " \$5 bill(s)");                 double five = remainderE * 5;         remainderF = remainderD - five;         }             if(remainderF >= 2 && remainderF < 5){         double answerE = remainderF / 2;         double remainderG = Math.floor(answerE);                 System.out.println(remainderG + " toonie(s)");                 double two = remainderG * 2;         remainderH = remainderF - two;       }             if(remainderH >= 1 && remainderH < 2){         double answerF = remainderH / 1;         double remainderI = Math.floor(answerF);                 System.out.println(remainderI + " loonie(s)");                         double one = remainderI * 1;         remainderJ = remainderH - one;         System.out.println(remainderJ);     }                     }   }```
• 09-05-2013, 12:06 AM
jim829
Re: Problem with creating a program that calculates money
Look at this section of code.

Code:

```if (remainder >= 20 && remainder < 50) {         double answerB = remainder / 20;         double remainderA = Math.floor(answerB);         System.out.println(remainderA + " \$20 bill(s)");         double twenty = remainderA * 20;         remainderB = remainder - twenty;       }       if (remainderB >= 10 && remainderB < 20) {         double answerC = remainderB / 10;         double remainderC = Math.floor(answerC);         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
• 09-05-2013, 01:07 AM
incxx
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?
Code:

```import java.io.*; public class ChangeProgram {   public static void main (String [] args) throws IOException   {     BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));         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: ");     inputA = myInput.readLine();     input = Double.parseDouble(inputA);             if (input >= 50 || input < 100){       double answer = input / 50;       double fifty = Math.floor(answer);             System.out.println(fifty + " \$50 bill(s)");       remainder = input - 50;     }             if(remainder >= 20 || remainder < 50){         double answerB = remainder / 20;         double remainderA = Math.floor(answerB);                 System.out.println(remainderA + " \$20 bill(s)");                 double twenty = remainderA * 20;         remainderB = remainder - twenty;       }                   if(remainderB >= 10 || remainderB < 20){         double answerC = remainderB / 10;         double remainderC = Math.floor(answerC);                 System.out.println(remainderC + " \$10 bill(s)");                 double ten = remainderC * 10;         remainderD = remainderB - ten;       }             if(remainderD >= 5 || remainderD < 10){         double answerD = remainderD / 5;         double remainderE = Math.floor(answerD);                 System.out.println(remainderE + " \$5 bill(s)");                 double five = remainderE * 5;         remainderF = remainderD - five;         }             if(remainderF >= 2 || remainderF < 5){         double answerE = remainderF / 2;         double remainderG = Math.floor(answerE);                 System.out.println(remainderG + " toonie(s)");                 double two = remainderG * 2;         remainderH = remainderF - two;       }           if(remainderH >= 1 || remainderH < 2){         double answerF = remainderH / 1;         double remainderI = Math.floor(answerF);                 System.out.println(remainderI + " loonie(s)");                         double one = remainderI * 1;         remainderJ = remainderH - one;             }           if (remainderJ >= 0.25 || remainderJ < 1){         double answerG = remainderJ / 0.25;         double remainderK = Math.floor(answerG);                 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;         double remainderM = Math.floor(answerH);               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;         double remainderO = Math.floor(answerI);               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;         double remainderQ = Math.floor(answerJ);               System.out.println(remainderQ + " penny(ies)");               double penny = remainderQ * 0.01;         remainderR = remainderP - penny;           }                       }   }```
• 09-05-2013, 01:13 AM
jim829
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