By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
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
  1. MonthlyIntRate = AnnualIntRate / 12.0;
  2.     monthlyPayment = ((MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)) / (pow(1 + MonthlyIntRate, numPayments) - 1.0)) * loanAmount;
  3.  
Mar 22 '07 #1
Share this Question
Share on Google+
7 Replies


sicarie
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
  1. MonthlyIntRate = AnnualIntRate / 12.0;
  2.     monthlyPayment = ((MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)) / (pow(1 + MonthlyIntRate, numPayments) - 1.0)) * loanAmount;
  3.  
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

sicarie
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
  1. monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)
  2. monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0)
  3. monthlyPayment *= loanAmount;
  4.  
Mar 22 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
So am I on the right track with this formula below:
Expand|Select|Wrap|Line Numbers
  1. monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)
  2. monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0)
  3. monthlyPayment *= loanAmount;
  4.  
I duno, did that help?

I would start with :
Expand|Select|Wrap|Line Numbers
  1. montlyPayment = pow(1+monthlyIntRate,numPayments);
  2. cout << monthlyPayment;
  3. montlyPayment *= MontlyIntRate;
  4. cout << montlypayment;
  5. ....
  6.  
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
  1. monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)
  2. monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0)
  3. monthlyPayment *= loanAmount;
  4.  




Tried the above formula and it is not working.....
Mar 22 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677

I would start with :
Expand|Select|Wrap|Line Numbers
  1. montlyPayment = pow(1+monthlyIntRate,numPayments);
  2. cout << monthlyPayment;
  3. montlyPayment *= MontlyIntRate;
  4. cout << montlypayment;
  5. ....
  6.  
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

Post your reply

Sign in to post your reply or Sign up for a free account.