Huy
Huy

Reputation: 11206

Simple calculation not displaying correct value

I wrote a simple program that receives several inputs and it does a future investment calculation.

However, for some reason, when I enter the following values: investment = 1 interest = 5 year = 1

I get Your future value is 65.34496113081846 when it should be 1.05.

import java.util.*;

public class futurevalue
{
  public static void main(String[] args) 
  {

    Scanner sc = new Scanner(System.in);

    System.out.println("This program will calculate your future investment value"); 

    System.out.println("Enter investment amount: ");
    double investment = sc.nextDouble();

    System.out.println("Enter annual interest amount: ");
    double interest = sc.nextDouble();

    System.out.println("Enter number of years: ");
    int year = sc.nextInt();


    double futureValue = investment * (Math.pow(1 + interest, year*12));

    System.out.println("Your future value is " + futureValue);


  }
}

Found my mistake. I divided my interest twice.

Upvotes: 0

Views: 1028

Answers (3)

trutheality
trutheality

Reputation: 23465

Yes, your main error is not dividing by 100 to convert from percents to proportion, but you have another error:

If you have an APR of 5% the formula you need to use to calculate a compounding monthly interest isn't 5%/12, it's

(0.05+1)^(1/12)-1

And then the return for that investment ends up being:

1 * ( (0.05+1)^(1/12)-1 +1 )^(1 * 12) =
1 * ( (0.05+1)^(1/12) )^(12) =
1 * ( 0.05+1 ) = 1.05

exactly.

Upvotes: 0

JF4
JF4

Reputation: 488

How is the interest rate being entered? Should you not divide it by 100 before adding 1 inside Math.pow?

Example: Monthly interest = 1%, if you enter 1, your Math.pow would be Math.pow(1+1, year*12) which would be incorrect.

Upvotes: 1

Rens Verhage
Rens Verhage

Reputation: 5857

You should divide your interest by 100.

Upvotes: 2

Related Questions