446,171 Members | 1,037 Online Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

# Formula for calculating annual interest

 P: 4 I'm having some logic trouble (IE: I can't figure out how to do this) I have an assignment in which I need to calculate annual interest. IE: P1 = P0 + P0*INT (interest) = P0*(1+INT) P2 = P1 + P1*INT = P1*(1+INT) = P0 * (1+INT) * (1+INT) = P0 * (1+INT)^2 P3 = P2 + P2*INT = P2 *(1+INT) = P0 * (I+INT)^3 And so on. essentially, here is what I have so far. \*Notes The initial statement is supposed start at 1700, it's supposed to compound for 60 years, and it has to be a 10% interest rate. *\ Expand|Select|Wrap|Line Numbers # include  # include    using namespace std;   int main (int argc, char* argv[]) {     int i; // counter     double Initialstatement = 1700;     int    Numberofyear = 60;     double Interest = 0.1;     double Totalamountyr;     double TotalamountFinal;    for (i = 0; i < Numberofyear; i++)         {              Initialstatement = Initialstatement * Interest + Initialstatement;              TotalamountFinal = Initialstatement + (Initialstatement * Numberofyear);              }         cout << fixed << showpoint;         cout << setprecision(2);         cout << TotalamountFinal << "\n";           system ("PAUSE");         return 0; } This doesn't really do the correct math for "TotalamountFinal". Instead of acquiring interest from the previous year each year and adding it, it goes off in the wrong direction the minute i = 1 (or more than 1 year for the # of years) So, how can I set this up using loops, and not using powers? My professor won't let me use power functions. Sep 25 '06 #1
4 Replies

 10K+ P: 13,264 I'm having some logic trouble (IE: I can't figure out how to do this) I have an assignment in which I need to calculate annual interest. IE: P1 = P0 + P0*INT (interest) = P0*(1+INT) P2 = P1 + P1*INT = P1*(1+INT) = P0 * (1+INT) * (1+INT) = P0 * (1+INT)^2 P3 = P2 + P2*INT = P2 *(1+INT) = P0 * (I+INT)^3 And so on. essentially, here is what I have so far. \*Notes The initial statement is supposed start at 1700, it's supposed to compound for 60 years, and it has to be a 10% interest rate. *\ Expand|Select|Wrap|Line Numbers # include  # include    using namespace std;   int main (int argc, char* argv[]) {     int i; // counter     double Initialstatement = 1700;     int    Numberofyear = 60;     double Interest = 0.1;     double Totalamountyr;     double TotalamountFinal;    for (i = 0; i < Numberofyear; i++)         {              Initialstatement = Initialstatement * Interest + Initialstatement;              TotalamountFinal = Initialstatement + (Initialstatement * Numberofyear);              }         cout << fixed << showpoint;         cout << setprecision(2);         cout << TotalamountFinal << "\n";           system ("PAUSE");         return 0; } This doesn't really do the correct math for "TotalamountFinal". Instead of acquiring interest from the previous year each year and adding it, it goes off in the wrong direction the minute i = 1 (or more than 1 year for the # of years) So, how can I set this up using loops, and not using powers? My professor won't let me use power functions. What is TotalamountFinal supposed to be? Isn't it just the final value of Initialstatement in your loop? Sep 25 '06 #2

 P: 4 I would LIKE TotalamountFinal to be the interest accumulated for the set # of years "Numberofyears" (which = 60). IE: Initial Amount*(1+INT)^60 or 1700*[(1+ .1)^60] Problem is, I don't know how to do this without USING powers (which I can't use) I need to find another way of mathematically calculating it out... Without typing each of the 60 lines for each year. Because the # of years will eventually be something I allow the user to choose. First I need to get the math right. Sep 25 '06 #3

 P: 4 Ok, FORGET MY LAST QUESTION, I've revamped my code and this is what I've got. I have a totally new question that's probably easier to answer. I didn't put all the code, just what's essential for this question, so there might be some extra ints and doubles you don't understand. Should still be easy to figure out. Expand|Select|Wrap|Line Numbers # include  # include    using namespace std;   int main (int argc, char* argv[]) {     int i; // counter     double Initialstatement;     double InitialstatementTWO;     int    Agestart;          // Age you start at     int    AgeWithdraw;       // Age you withdraw money from     int    Numberofyear;      // The Number of years Interest will be calcualted     int    Numberofyearplus;  // The Number of years plus 1     double x;     double y;     double Interest = 0.1;    // interest rate     double Interestperyear;   // The amount of interest per year gained off of 61 years       cout << "What is the Age of the individual when the initial amount \n"          << "is deposited? \n";     cin >> Agestart;       cout << "What is the Age of the individual when the money will be \n"          << "withdrawn? \n";     cin >> AgeWithdraw;       Initialstatement == InitialstatementTWO;     Numberofyear = AgeWithdraw - Agestart;     cout << "Interest will be calculated for " << Numberofyear << " years.\n";     cout << "\n";     cout << "What is the initial statement to be deposited?\n";     cin >> Initialstatement;         for (i = 0; i < Numberofyear; i++)         {              InitialstatementTWO = InitialstatementTWO * Interest + InitialstatementTWO;           }        cout << fixed << showpoint;         cout << setprecision(2);         cout << InitialstatementTWO << "\n";         system ("PAUSE");         return 0; } So the question is... Why do I get a "0.00" for InitialstatementTWO ? Sep 25 '06 #4

 P: 4 I think the problem lies here. Expand|Select|Wrap|Line Numbers     cout << "What is the initial statement to be deposited?\n";     cin >> Initialstatement;     Initialstatement = InitialstatementTWO;       for (i = 0; i < Numberofyear; i++)         {              InitialstatementTWO = InitialstatementTWO * Interest + InitialstatementTWO;         } I need to find a way to make InitialstatementTWO equivalent to Initialstatement. initialstatement will change in the loop over time, but I need to refer to the original " cin >> Initialstatement" and output it for the assignment. So, I thought I"d make initialstatement equal to another variable, then use THAt variable "InitialstatementTWO to calculate interest over time in the loop. Apparently, I can't do this because its' not recognizing initialstatementTWO as being equal to Initialstatement when I say Initialstatement = InitialstatementTWO. Any ways around this? Sep 25 '06 #5 