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 2010/05/24 5:21 UTC
Read the original article Hit count: 291

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

double calculateInterest(double startUp, double rate, double duration) 
{ 
  double cpdInterest = (duration*startUp)*(1 + rate); 

  if (duration == 0)  
{ 
  return cpdInterest; 
} 
  else if (duration < 0)  
  { 
  cout << "Please enter a valid year"; 
   } 
else 
 { 
  calculateInterest(cpdInterest,rate,duration); 
 } 

  return cpdInterest; 

}

© Stack Overflow or respective owner

Related posts about c++