I am asking for three inputs from the user:

  1. How much money do you want to retire?(Savings goal)
  2. Initial Investment
  3. Annual contribution
  4. 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.

  • $\begingroup$ A clarification: Aren't you asking the answer in the first input? $\endgroup$ – GoodDeeds Feb 15 '16 at 15:59
  • $\begingroup$ @GoodDeeds I have updated the sample input and output. $\endgroup$ – Farhan Ishraq Feb 15 '16 at 16:07
  • $\begingroup$ What is the meaning of the "savings goal" if the output of the algorithm shows you have less than that amount "in the bank" when you retire? $\endgroup$ – David K Feb 15 '16 at 16:21

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.