Yusuf Ning
Yusuf Ning

Reputation: 65

Calculating investment duration in Java using simple interest formula

So the task that Im trying to do is to find the number of years taken for a principal to reach a certain value. say for example I start with $5000 and I want to accumulate $15000 with 10% interest rate/year. I want to find how long is the duration of that investment

this is what I have done so far

package com.company;

import java.util.Scanner;


public class InvestmentDuration {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println ("Initial Investment: ");
    double investment = input.nextDouble();

    System.out.println ("Rate as decimals: ");
    double rate = input.nextDouble();

    System.out.println ("Future Value: ");
    double FutureValue = input.nextDouble();

    double T = 0; //Initialise Duration//

    double EndValue = investment * Math.pow ((1+rate), T); //Formula of Simple Interest//

     while (EndValue < FutureValue) {
        T += 1.0;

         if (investment * Math.pow((1 + rate), T) == FutureValue);

        System.out.println("The Number of years it takes to accumulate $" + FutureValue + " is " + T + " years");
    }


}

output:

The Number of years it takes to accumulate $15000.0 is 1.0 years
The Number of years it takes to accumulate $15000.0 is 2.0 years
The Number of years it takes to accumulate $15000.0 is 3.0 years
The Number of years it takes to accumulate $15000.0 is 4.0 years
The Number of years it takes to accumulate $15000.0 is 5.0 years
The Number of years it takes to accumulate $15000.0 is 6.0 years
The Number of years it takes to accumulate $15000.0 is 7.0 years
The Number of years it takes to accumulate $15000.0 is 8.0 years
The Number of years it takes to accumulate $15000.0 is 9.0 years
The Number of years it takes to accumulate $15000.0 is 10.0 years
The Number of years it takes to accumulate $15000.0 is 11.0 years
The Number of years it takes to accumulate $15000.0 is 12.0 years

how do I print just the last line?

Upvotes: 1

Views: 2189

Answers (3)

SilverNak
SilverNak

Reputation: 3381

You need to use a loop (for or while). In this loop you can increment the the year and calculate the new value.

Note that I did some changes to the variables:

  • Since you wanted to an integer loop, the type of T is int
  • I changed EndValue and FinalValue to endValue and finalValue respectively. The Java naming conventions are camelCase with small first letter for variable names.
  • I think years is a better name than T, but that's my personal opinion. If you decide to stay with T, at least it should be a small letter t

Then you can use the following code. Saving endValue in a variable is not really necessary, since it is used only once. So it could be inlined. But I decided to stay close to your question.

    int years = 0;

    double endValue = investment;

    while (endValue < futureValue) {
        years++;
        endValue = investment * Math.pow((1 + rate), years);
    }

You should be aware that after this loop, years is the number of full years where endValue is greater than or equal to futureValue. That means you can't have something like 3.5 years as result. If you want to calculate that, you should use the solution of Henry.

Upvotes: 1

Henry
Henry

Reputation: 43738

The easiest solution is with a bit of mathematics:

Math.log(goal/start) / Math.log(1+rate/100.0)

where goal and startis the amount at the end and at the beginning respectively and rate is the interest rate in percent.

Upvotes: 3

jr593
jr593

Reputation: 277

Lots wrong here... Firstly, your IF statement : It has no action because you have closed it with a semi-colon. Also, endvalue is unlikely to equal futurevalu, partly because of floating point accuracy, and partly because is unlikely to be a whole number of years. So I would try something like :

double T = 0.0;
while(investment * Math.pow ((1+rate), T) < FutureValue) {
   T += 1.0;
}
System.out.println ("The Number of years it takes to accumulate the Future Value is: " +T+);

Or you could re-jig the formula to calculate T directly, of course.

Upvotes: 0

Related Questions