Search code examples
javaloopsfinanceratecomputational-finance

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?


Solution

  • 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.