449,168 Members | 1,171 Online Need help? Post your question and get tips & solutions from a community of 449,168 IT Pros & Developers. It's quick & easy.

# trouble with a formula in source code

 100+ P: 143 I'm having trouble getting a mathmatical formula to work in my code. I am supposed to write a program in java that calculates the ending balance each month. The user is supposed to input the monthly payment, the annual interest rate, and the total months required to pay off the loan. I am to use the formula: Balance(pn) = payment * (1 - (1 + mir) ^ (pn - tp)) / mir payment = monthly payment mir = monthly interest rate pn = payment number tp = total number of payments ^ = raised to the power of (the exponential operator - code will use the pow method for this task) Here's my code so far: [HTML]import java.util.Scanner; public class CarPayment { private double balance; //balance for payoff private double payment; //monthly payment amount private double mir; //monthly interest rate private double pn; //payment number private double tp; //total number of payments required for payoff public void inputdata() { Scanner input = new Scanner(System.in); System.out.println("Enter the monthly payment:"); payment = input.nextDouble(); System.out.println("Enter the annual interest rate:"); mir = input.nextDouble(); System.out.println("Enter the number of months required for pay off:"); tp = input.nextDouble(); } public void schedule() { System.out.printf("%s%20s\n", "After payment number", "Payoff"); for (int i = 1; i <= tp; i++) { // balance = payment * (1 - (1 + mir) ^ (pn - tp)) / mir; balance = payment * (1 - Math.pow((1 + mir),(pn - tp))) / mir; System.out.println(i + " \$" + balance); } //end for } //end schedule } //end CarPayment class public class CarPaymentTester { public void run() { CarPayment pay = new CarPayment(); pay.inputdata(); pay.schedule(); } //end run public static void main(String[] args) { CarPaymentTester t = new CarPaymentTester(); t.run(); } //end main } //end CarPaymentTester class[/HTML] the problem code is in the schedule method of the CarPayment class. Any help will be appreciated. Thanks. Feb 7 '07 #1
8 Replies

 10K+ P: 13,264 You did not initialize pn so IMO pn is always 0 everytime. I think pn should be int not double and should be used as Expand|Select|Wrap|Line Numbers  for (int i = 1; i <= tp; i++) {          balance = payment * (1 -  Math.pow((1 + mir),(i - tp))) / mir;       System.out.println(i + \"         \$\" + balance);  }       So that pn starts from 1 to tp Feb 7 '07 #2

 P: 64 Final=Start(1+(interest in a decimal(10%=.10)/YearDenoms(Per year is 1, per month is 12))^Years/YearDenoms Thats an effective formula I learned last year in Math for finding interest. Feb 7 '07 #3

 P: 64 Scratch that power part, its the rate*year denoms, sorry...not Years/year denoms, if you have questions about it I will clarify any of them Feb 7 '07 #4

 100+ P: 143 r035198x, Thanks for the help, but the problem is still there. Your formula with pn initialized gets one of the same results I was getting. I'm still searching for a solution. JavaStudent07, I'm sure your formulas will work, but my instructor specified the basic formula and the fact that it must utilize the Math.pow() method. Thanks for the input none the less. Feb 7 '07 #5

 Expert 100+ P: 1,510 r035198x, Thanks for the help, but the problem is still there. Your formula with pn initialized gets one of the same results I was getting. I'm still searching for a solution. JavaStudent07, I'm sure your formulas will work, but my instructor specified the basic formula and the fact that it must utilize the Math.pow() method. Thanks for the input none the less. if schedule() is Expand|Select|Wrap|Line Numbers     public void schedule()     {         System.out.printf("%s%20s\n", "After payment number", "Payoff");           for (int pn = 1; pn <= tp; pn++)         {  //         balance = payment * (1 - (1 + mir) ^ (pn - tp)) / mir;             balance = payment * (1 -  Math.pow((1 + mir),(pn - tp))) / mir;             System.out.println(pn + "         \$" + balance);         }   //end for     }    //end schedule }   //end CarPayment class   when run I get Enter the monthly payment: 100 Enter the annual interest rate: .1 Enter the number of months required for pay off: 10 After payment number Payoff 1 \$575.9023816275154 2 \$533.4926197902669 3 \$486.84188176929354 4 \$435.52606994622283 5 \$379.07867694084507 6 \$316.98654463492954 7 \$248.68519909842243 8 \$173.55371900826455 9 \$90.90909090909093 10 \$0.0 which looks reasonable - any idea what the answer should be? Feb 8 '07 #6

 100+ P: 143 Yes, that looks reasonable, but I was given test criteria to ensure the formula is correct. If the monthly payment is 165.25, the total number of payments is 36, and the interest rate is 9%, the balance after the first payment is 5070.31. When I run these numbers I get a first payment of 1746.16. Any ideas? Thanks for helping me dig. Feb 8 '07 #7

 Expert 100+ P: 1,510 Yes, that looks reasonable, but I was given test criteria to ensure the formula is correct. If the monthly payment is 165.25, the total number of payments is 36, and the interest rate is 9%, the balance after the first payment is 5070.31. When I run these numbers I get a first payment of 1746.16. Any ideas? Thanks for helping me dig. I think your problem is you are asking for the annual interest rate which is 9% and the calculation uses the monthly interest rate which is .0075 using your data with this value a run gives Enter the monthly payment: 165.25 Enter the annual interest rate: .0075 Enter the number of months required for pay off: 36 After payment number Payoff 1 \$5070.308952032767 2 \$4943.086269173012 3 \$4814.909416191807 4 \$4685.771236813245 5 \$4555.664521089341 6 \$4424.582004997514 7 \$4292.516370034991 8 \$4159.460242810253 9 \$4025.4061946313295 10 \$3890.3467410910625 11 \$3754.2743416492444 12 \$3617.1813992116136 13 \$3479.0602597057 14 \$3339.9032116534904 15 \$3199.70248574089 16 \$3058.450254383945 17 \$2916.138631291825 18 \$2772.7596710265116 19 \$2628.3053685592095 20 \$2482.7676588234012 21 \$2336.1384162645772 22 \$2188.40945438656 23 \$2039.572525294457 24 \$1889.619319234164 25 \$1738.5414641284187 26 \$1586.3305251093814 27 \$1432.978004047701 28 \$1278.4753390780572 29 \$1122.8139041211405 30 \$965.985008402048 31 \$807.979895965062 32 \$648.7897451847983 33 \$488.40566827368485 34 \$326.8187107857351 35 \$164.01985111662685 36 \$0.0 Feb 8 '07 #8

 100+ P: 143 OOOOOO. Good catch. I can work with that. Thanks. I know where to go from here. Feb 8 '07 #9 