Translating a math expression

I need to translate this expression into C++ code, without using cmath functions.

Payment = ((Rate(1+Rate)^N)/((1+Rate)^N - 1)) * L

my particular concern though is only with the (Rate + 1)^N

I need to loop this expression so as to evaluate it from N=1 to N=24. How can i do that in an elegant way?

Thank you.
bump. anyone?
once again BUMP for this.
I need to loop this expression so as to evaluate it from N=1 to N=24. How can i do that in an elegant way?

Use a for loop:
1
2
3
  for (int N=1; N<=24; N++)
  {  //  perform calculation
  }


Sry, I still dont understand. What should I write in the calculation part?

(rate + 1) * (rate + 1)

wouldn't that give me an answer that is more than (rate + 1)^N...

Raising a number to the power N where N is an integer is simply a matter of repeated multiplication.

Although you could write a separate function to carry out a loop to do this, if you already have a looping structure in your program, you can just use some value which is set to an initial value of 1.0, then each time around the loop, you multiply it by the number.
Do you have any C or C++ experience?
Your question sounds like a homework assignment and it's the policy of this site not to do homework assignments for people. You should be trying to write this yourself.

Actually it is a homework assignment, but I haven't asked you to write my homework assignment for me have I? Like anyone else I have stumbled on something I don't understand and I sought help. I am not very good with programming and mathematics, I am a History major. Nevertheless here i am putting in the time and the effort and learning. I am not asking someone to write my program for me, just to guide me in understanding a concept.

Thank you.

anyway I have come up with this, will this work?

for(int N=1; N<=24; N++)

N *=(1 + Rate)
It won't work as written, because you are trying to use the variable N for two completely separate purposes (a) to control how many times the loop is executed and (b) to store the value of the number raised to a particular power. In addition, N is of type int, which is fine for the loop counter, but the other variable would be better as a type double.

Chervil wrote:
use some value which is set to an initial value of 1.0, then each time around the loop, you multiply it by the number.

before the loop begins, define a variable of type double, set its value to 1.0.
Then use that inside the loop like this:
number *= (1 + Rate);
Ok. Sweet. Thank you!
@DanStirletz

1
2
3
4
5
6
for(N=1; N<=24; N++){
    b[N]=pow(Rate,N);
    Payment[N] = ((Rate*b[N])/( b[N]- 1)) *L;
    
    cout<<"Payment no. "<<N<<": "<<Payment[N]<<endl;
    }
Topic archived. No new replies allowed.