I am asking for three inputs from the user:
- How much money do you want to retire?(Savings goal)
- Initial Investment
- Annual contribution
- Interest rate
And I want to calculate the amount you end up with in the bank and the retirement time?
Sample input: Savings goal: 1000000 Initial Investment: 5000 Annual contribution: 2500 Interest rate: 2.5 percent
Output: You can retire in 96 years.
If we for a moment ignore the annual contribution, we can write the familiar $$P(n)=P_0(1+r)^n$$ where $P(n)$ is the amount of money at year $n$, $P_0$ is the starting sum, $r$ is the interest rate and $n$ is the number of years.
If we let the annual contribution be $P_{ac}$, we just add a term and we're done: $$P(n)=P_0(1+r)^n+\frac{P_{ac}(1+r)^n-1}{r}$$
If you want to input a specific savings goal and want to find out how much time it would take to reach it, just put the goal in $P(n)$'s place and solve for $n$.
Note that this does not correct for inflation.
Hope that helps! If you have any questions, please leave a comment below.