Compound Interest with recursion help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mastern200
    New Member
    • Oct 2006
    • 16

    #1

    Compound Interest with recursion help

    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.

    Code:
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    float initialDeposit=0;
    int depositMonth=0;
    float interestRate=0;
    float total=0;
    void mainFunction();
    void endingBalance();
    bool goAgain();
       
    int main() {
        do{
        system("cls");
        mainFunction();
        endingBalance();
       
        }while(goAgain());
        cin.get();
        cin.get();
        return 0;
    }
    
    void mainFunction(){
        cout<<"What is the initial deposit? ";
        cin>>initialDeposit;
        cout<<endl;
        cout<<"How many months is the deposit? ";
        cin>>depositMonth;
        cout<<endl;
        cout<<"What is the annual interest rate? ";
        cin>>interestRate;
        cout<<endl;
    }
    
    void endingBalance(){
         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<<".";
         cout<<endl;
         }
    
    bool goAgain()
    {
         char answer;
    
         cout << "Would you like to go again?";
         cin >> answer;
    
    
        while(answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N')
        {
          cout << "Invalid choice.  Would you like to go again?";
          cin >> answer;
        }
    
        cout << endl;
       
    
        if(answer == 'y' || answer == 'Y')
          return true;
        else
          return false;
    }
  • enri
    New Member
    • Feb 2007
    • 10

    #2
    mastern2000,

    this would probably do the trick:

    Code:
    double Compound(double capital, double interest, int months)
    {
         if (months==1)
              return capital*(1+interest);
         else
              return (1+interest)*Compound(capital,interest, months--);
    }
    Last edited by enri; Feb 15 '07, 07:30 PM. Reason: indentation

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by enri
      mastern2000,

      this would probably do the trick:

      Code:
      double Compound(double capital, double interest, int months)
      {
           if (months==1)
                return capital*(1+interest);
           else
                return (1+interest)*Compound(capital,interest, months--);
      }
      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

      Comment

      Working...