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

# Incorrect value being returned after calculation.

 P: 49 I am writing a program that calculates the monthly payment and interest of a loan. I am having trouble getting the program to calculate the Monthly Payment. The correct formula given by my textbook is as follows: Payment = Rate * (1 + Rate)^N ---------------------------- * L ( (1 + Rate)^N - 1) Rate, being the monthly interest rate N, being the number of payments L, being the loan amount. Expand|Select|Wrap|Line Numbers MonthlyIntRate = AnnualIntRate / 12.0;     monthlyPayment = ((MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)) / (pow(1 + MonthlyIntRate, numPayments) - 1.0)) * loanAmount;   Mar 22 '07 #1
7 Replies

 Expert Mod 2.5K+ P: 4,677 I am writing a program that calculates the monthly payment and interest of a loan. I am having trouble getting the program to calculate the Monthly Payment. The correct formula given by my textbook is as follows: Payment = Rate * (1 + Rate)^N ---------------------------- * L ( (1 + Rate)^N - 1) Rate, being the monthly interest rate N, being the number of payments L, being the loan amount. Expand|Select|Wrap|Line Numbers MonthlyIntRate = AnnualIntRate / 12.0;     monthlyPayment = ((MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)) / (pow(1 + MonthlyIntRate, numPayments) - 1.0)) * loanAmount;   Have you tried breaking the individual calculations up into several lines, to make sure order of operations is how you want it? Mar 22 '07 #2

 P: 49 No....I am not sure how to break up a formula on several lines....got any examples? Mar 22 '07 #3

 Expert Mod 2.5K+ P: 4,677 No....I am not sure how to break up a formula on several lines....got any examples? You might want to talk to your teacher then, that could be a problem. Take the individual calculations (addition, subtraction, multiplication, powers...), and do them individuallty Example: int = 1 + 2 * 3; to int = 2*3; int += 1; Mar 22 '07 #4

 P: 49 Have you tried breaking the individual calculations up into several lines, to make sure order of operations is how you want it? So am I on the right track with this formula below: Expand|Select|Wrap|Line Numbers monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments) monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0) monthlyPayment *= loanAmount;   Mar 22 '07 #5

 Expert Mod 2.5K+ P: 4,677 So am I on the right track with this formula below: Expand|Select|Wrap|Line Numbers monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments) monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0) monthlyPayment *= loanAmount;   I duno, did that help? I would start with : Expand|Select|Wrap|Line Numbers montlyPayment = pow(1+monthlyIntRate,numPayments); cout << monthlyPayment; montlyPayment *= MontlyIntRate; cout << montlypayment; ....   and literally do each calculation separately, printint out after each (that way you can debug/check the values). From there, you can combine them slowly until you see where the errors are coming from (I'm just guessing order of operations). Mar 22 '07 #6

 P: 49 So am I on the right track with this formula below: Expand|Select|Wrap|Line Numbers monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments) monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0) monthlyPayment *= loanAmount;   Tried the above formula and it is not working..... Mar 22 '07 #7

 Expert Mod 2.5K+ P: 4,677 I would start with : Expand|Select|Wrap|Line Numbers montlyPayment = pow(1+monthlyIntRate,numPayments); cout << monthlyPayment; montlyPayment *= MontlyIntRate; cout << montlypayment; ....   and literally do each calculation separately, printint out after each (that way you can debug/check the values). From there, you can combine them slowly until you see where the errors are coming from (I'm just guessing order of operations). Did you try this? Mar 22 '07 #8