Reputation: 405
When I enter 1000 for investment amount 4.25 for monthly interest rate and 1 for years, why do I get the result 4.384414858452464E11 instead of the expected 1043.34?
import java.util.Scanner;
public class FinancialApplicationFutureInvestment_13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter investment amount: ");
int investmentAmount = input.nextInt();
System.out.println("Enter monthly interest rate: ");
double monthlyInterestRate = input.nextDouble();
System.out.println("Enter number of years");
int numOfYears = input.nextInt();
double futureInvestmentValue = investmentAmount *
(Math.pow(1 + monthlyInterestRate, numOfYears * 12));
System.out.println("Accumulated value is: " + futureInvestmentValue);
double test = Math.pow(1 + monthlyInterestRate, numOfYears * 12);
System.out.println(test);
}
}
Upvotes: 1
Views: 931
Reputation: 11
you should use casting in System.out.println (double to int) -- (int)(futureInvestmentValue * 100) / 100.0
Upvotes: 1
Reputation: 684
well, you compute 1+4.25 (5.25) as monthly interest rate, instead of 1+(4.25/100) .
Upvotes: 2
Reputation: 18568
the formula is
A = P(1+r/100)^n
so it should be
investmentAmount * (Math.pow(1 + (monthlyInterestRate/100), numOfYears * 12));
Upvotes: 2
Reputation: 137810
1 + monthlyInterestRate
Is monthlyInterestRate
a raw factor, or is it expressed in percentage points?
Try dividing by one hundred.
Upvotes: 2