Money problem

This is a discussion on Money problem within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm stuck with a new problem here, and I would really appreciate some help. lets see... what I want ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22

    Money problem

    Hi! I'm stuck with a new problem here, and I would really appreciate some help.
    lets see... what I want to know is in how many years will the variable money duplicate itself, having an interest rate of 5% per year . For example, if I start with money = 5, how maney years it will take to money = 10.
    Ok, I wrote this code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        float money;
        float money2;
        float money3;
        double rate;
        int x; //years
        
        rate = 0.05;
        
        
        cout<<"Write money: ";
        cin>> money;
    
        money2 = money * 2;
        money3 = money;
        x = 0;
        
        while (money <= money2)
        {
              money = money + (money * rate);
              x++;
                 
        }
      
      
      cout<<"\n";
      cout<<x<<"\n"; 
      cout<<money3<<"\n";
      cout<<money2<<"\n";
      cout<<money<<"\n";
      
      system("pause");
      return 0;
        
    }
    the money2 and money3 variables are unnecessary but i put them just to make sure everything was ok.
    The input is 5, and the output goes like this:

    15
    5 ( unnecessary )
    10 ( unnecessary)
    10.395 (in this case, WHY is the value higher than 10??? I mean, I put in the code:
    money <= money2, so why is it higher?)

    And the other thing is, why is x (the years) always 15?? if input 2, the output is the same: 15, and if i put 1000 its again 15.

    So... that's the two questions I'm stuck with. What I'm I doing wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,597
    what I want to know is in how many years will the variable money duplicate itself, having an interest rate of 5&#37; per year . For example, if I start with money = 5, how maney years it will take to money = 10.
    You could make use of a formula for calculating compound interest. Doing some simple manipulation of a formula shown on Wikipedia, I derived:
    n = log(f / p) / log(1 + i)
    where n is the number of periods (years in your case), f is the future value, p the present value, and i the interest rate. log() is available in <cmath>.

    10.395 (in this case, WHY is the value higher than 10??? I mean, I put in the code:
    money <= money2, so why is it higher?)
    On the last iteration, money was increased to be higher in value than money2, hence the loop terminated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I don't think it's wrong as long as you're ok with getting it in whole years necessary.

    as to why you're getting the same answer every time, I think it's because algebraically, the input money value cancels out or something.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Tesnik View Post
    And the other thing is, why is x (the years) always 15?? if input 2, the output is the same: 15, and if i put 1000 its again 15.
    Of course it is - because that's how long it takes for an interest rate of 5&#37; to at least double your money. it doesn't matter how much money you start with, whether its &#163;1 or &#163;1 billion..

    Interest rates are based on multiplication by the same percentage (multiplier) each year (Well, in reality they change, but assume its constant for this example), So if you forget about the initial amount of money, all you're left with is a question of "How many times do I have to multiply 1.05 by itself before it becomes a value greater than or equal to 2" and the answer is, as you have discovered, 15.

    interest rate of 5% is a multiplier of 1.05 because you keep the existing 100% = 1 and add on 5% = 0.05 every year.. so 100%+5% = 1.05)

    The calculation could be shortened to, 1.05^15 (1.05 to the power of 15) which equals 2.078 (Approx)

    so,
    &#163;1 * 2.078 = &#163;2.078
    &#163;1000 * 2.078 = &#163;2078
    &#163;1000,000 * 2.078 = &#163;2,078,000



    Edit - If you wanted to make your problem a bit more interesting, try changing the annual interest rate, and see how the time taken varies
    Last edited by Bench82; 08-23-2007 at 03:39 PM.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    thanks all of you for the help!

    Bench82 very good explanation
    Last edited by Tesnik; 08-23-2007 at 04:50 PM.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    10.395 (in this case, WHY is the value higher than 10??? I mean, I put in the code:
    money <= money2, so why is it higher?)
    Because at the end of year 14 you 've got slightly less than $10 and at the end of year 15 you have slightly more than $10. You never have exactly twice what you started with.

    And the other thing is, why is x (the years) always 15?? if input 2, the output is the same: 15, and if i put 1000 its again 15.
    It doesn't matter if you invest $1 or $1000. Your investment will double in 15 years.

    You could make use of a formula for calculating compound interest. Doing some simple manipulation of a formula shown on Wikipedia, I derived:
    n = log(f / p) / log(1 + i)
    where n is the number of periods (years in your case), f is the future value, p the present value, and i the interest rate. log() is available in <cmath>.
    Assuming this is a homework assignment (or exercise) having to do with loops, I wouldn't recommend using a financial formula "shortcut". If it's a real-world fincancial application I would NOT use a loop.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    As to why the period is always the same... it's because you are observing the effects of the http://en.wikipedia.org/wiki/Rule_of_72
    with regards to compound interest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Problem with the purchase function
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 02-07-2006, 10:03 AM
  5. Problem With Code
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 02-04-2006, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21