formulas

how would you translate this formula to work in c++ and can u explain why you did what you did because I am new to this.

(p * (1 + r) n * r) / ((1 + r) n - 1)

this is what I tried so far. do I need to split it into two variables?
p is the amount borrowed
• r is the monthly decimal interest rate
• n is the number of monthly payments in the payback period
1
2
3
4
5
6
7
  int amtBorrowed = 270000; //$
  double rateInterest = 5.125; //%
  int payPeriod = 30; //years
  double mustPay = (amtBorrowed * (1 + rateInterest)) payPeriod * rateInterest)) / (1 + rateInterest) payPeriod - 1);

  cout << fixed << setprecision(2) << mustPay <<endl;
  
(amtBorrowed * (1 + rateInterest)) HERE payPeriod * rateInterest))
See where I've written HERE in that line? There's something missing there. I'm guessing you want that to multiply, so it should perhaps be:
(amtBorrowed * (1 + rateInterest)) * payPeriod * rateInterest))


Likewise (1 + rateInterest) HERE payPeriod - 1)


Basically, just write it out much more explicitly.


(p * (1 + r) * n * r) / (((1 + r) * n) - 1)

Last edited on
The formula you have has an assumption of multiplication that C++ doesn't follow:

(1 + 2)(5 + 19)

In math, this is simply 3*24. In C++, this is an error. C++ does not recognize that you want to multiple the sum of 1+2 and 5+19. You need to do this:

(1 + 2)*(5 + 19)


Now, here's my interpretation of your formula in code. I've changed the variable names so it's easier:

1
2
3
4
5
6
7
8
9
int main()
{
	int p = 270000; //$
	double r = 5.125; //%
	int n = 30; //years
	double mustPay = (p * (1 + r) * n * r) / ((1 + r) * n - 1);

	cout << fixed << setprecision(2) << mustPay << endl;
}


I've only added a couple new '*' to make this work.


In your code, you'd added many parenthesizes that are unmatched.
if you were really uptight you could try to rearrange it to do less work, eg tmp = n+n*r and then (p*tmp*r)/(tmp -1)
but this is not saving enough computation to be worth a lot of over-thinking it. Alternately if the equation has a 'standard form' that people recognize, use that form.
Thank you very much!!
mortgageCalculator1.cpp:34:103: error: expected ')' before 'payPeriod'
34 | double mustPay = (amtBorrowed * (1 + rateInterest) * payPeriod * rateInterest) / ((1 + rateInterest) payPeriod - 1));
| ~ ^~~~~~~~~~
|


the compiler is not recognizing the second part of the equation, (I've tried adding more parentheses but just the same error with different characters.)
It's still the same issue that the others mentioned.

You have :
((1 + rateInterest) payPeriod - 1))

You want:
((1 + rateInterest) * payPeriod - 1))

Multiplication is not implicit in C++.
Last edited on
YUP I just saw that I need to pay more attention to detail! thank you again.
when i did the math on paper i got a different answer than my program, how can i find out why

int amtBorrowed = 270000; //$
float rateInterest = 5.125; //%
int payPeriod = 30; //years
double mustPay = (amtBorrowed * (1 + rateInterest) * payPeriod * rateInterest) / (1 + rateInterest) * payPeriod - 1;

the answer I got was = 1470.11
but my console gets = 1245374976.00

+++++++ i just needed to use "pow" for the exponents to get the math right.
Last edited on
You probably followed a different order of operations compared to how you coded it.
Interest rate is in %. You probably want to divide by 100.

Also, your current formula doesn't match your first post.
Hint: a / b + c == (a / b) + c, not a / (b + c).
Last edited on
its almost there but

with this, i am getting 17814.95 but i want 1470.11

int amtBorrowed = 270000; //$
float rateInterest = 5.125 / 100; //%
int payPeriod = 30; //years
double mustPay = (amtBorrowed * pow(1 + rateInterest, payPeriod) * rateInterest) / (pow(1 + rateInterest, payPeriod) - 1);

including the pow function helped but
im not sure what to do from here, to get where i need to get next
n is "the number of monthly payments in the payback period".
If the payback period is 30 years, and you do monthly payments, that is 30 * 12 = 360 payments.

And your interest rate is the monthly interest rate. The assignment's wording isn't exactly being clear here, so I'll just say you need to divide by 12.
I was using the wrong conversions for my interest rate and pay period
the code works perfectly otherwise.

float rateInterest = 5.125 / 100; //%
int payPeriod = 30; //years

vs

float rateInterest = .05125 / 12; //monthly rate%
int payPeriod = 360; // (12 * 30) //(12 months per year for 30 years)
Topic archived. No new replies allowed.