1. ## 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. 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. 3. 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. Originally Posted by Tesnik 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 5.  thanks all of you for the help!

Bench82 very good explanation  6. 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. 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 