recursive program

Posted by wilson88 on Stack Overflow See other posts from Stack Overflow or by wilson88
Published on 2010-05-24T05:14:05Z Indexed on 2013/10/20 9:54 UTC
Read the original article Hit count: 204

Filed under:
|

I am trying to make a recursive program that calculates interest per year.It prompts the user for the startup amount (1000), the interest rate (10%)and number of years(1).(in brackets are samples)

Manually I realised that the interest comes from the formula YT(1 + R)----- interest for the first year which is 1100.

2nd year YT(1 + R/2 + R2/2) //R squared

2nd year YT(1 + R/3 + R2/3 + 3R3/) // R cubed

How do I write a recursive program that will calculate the interest? Below is the function which I tried

//Latest after editing

double calculateInterest2(double start, double rate, int duration) 
{ 
    if (0 == duration) { 
        return start; 
    } else { 
        return (1+rate) * calculateInterest2(start, rate, duration - 1); 
    } 
} 

© Stack Overflow or respective owner

Related posts about c++

Related posts about recursion