By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,574 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,574 IT Pros & Developers. It's quick & easy.

Compound Interest with recursion help

P: 16
I need to make a program that calculates Compound Interest (compounded monthly). Thing is, it has to use recursion. I don't know how to implement it in though. This is the code i have so far.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4.  
  5. float initialDeposit=0;
  6. int depositMonth=0;
  7. float interestRate=0;
  8. float total=0;
  9. void mainFunction();
  10. void endingBalance();
  11. bool goAgain();
  12.  
  13. int main() {
  14.     do{
  15.     system("cls");
  16.     mainFunction();
  17.     endingBalance();
  18.  
  19.     }while(goAgain());
  20.     cin.get();
  21.     cin.get();
  22.     return 0;
  23. }
  24.  
  25. void mainFunction(){
  26.     cout<<"What is the initial deposit? ";
  27.     cin>>initialDeposit;
  28.     cout<<endl;
  29.     cout<<"How many months is the deposit? ";
  30.     cin>>depositMonth;
  31.     cout<<endl;
  32.     cout<<"What is the annual interest rate? ";
  33.     cin>>interestRate;
  34.     cout<<endl;
  35. }
  36.  
  37. void endingBalance(){
  38.      cout<<"With an initial deposit of $";cout<<initialDeposit;cout<<" and an interest rate of "; cout << interestRate; cout<<"% the balance at the end of ";cout<<depositMonth; cout<< " months is $"; cout<<total; cout<<".";
  39.      cout<<endl;
  40.      }
  41.  
  42. bool goAgain()
  43. {
  44.      char answer;
  45.  
  46.      cout << "Would you like to go again?";
  47.      cin >> answer;
  48.  
  49.  
  50.     while(answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N')
  51.     {
  52.       cout << "Invalid choice.  Would you like to go again?";
  53.       cin >> answer;
  54.     }
  55.  
  56.     cout << endl;
  57.  
  58.  
  59.     if(answer == 'y' || answer == 'Y')
  60.       return true;
  61.     else
  62.       return false;
  63.  
Feb 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: 10
mastern2000,

this would probably do the trick:

Expand|Select|Wrap|Line Numbers
  1.  
  2. double Compound(double capital, double interest, int months)
  3. {
  4.      if (months==1)
  5.           return capital*(1+interest);
  6.      else
  7.           return (1+interest)*Compound(capital,interest, months--);
  8. }
  9.  
Feb 15 '07 #2

AdrianH
Expert 100+
P: 1,251
mastern2000,

this would probably do the trick:

Expand|Select|Wrap|Line Numbers
  1.  
  2. double Compound(double capital, double interest, int months)
  3. {
  4.      if (months==1)
  5.           return capital*(1+interest);
  6.      else
  7.           return (1+interest)*Compound(capital,interest, months--);
  8. }
  9.  
Yes, it probably would, but I would like to point out that we are here to help one another by giving pointers, hints and methods of doing things, not give out the answers. Stating a potential algorithm in words or pseudo-code would be a better way of helping.

Not to be heavy handed, but please think about what you are writing before giving out a solution. Try and not do that again.

Thanks.


Adrian
Feb 15 '07 #3

Post your reply

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