Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula in Programming Exercise 2.21. Use the following method header: public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue(10000, 0.05/12, 5) returns 12833.59.

Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula in Programming Exercise 2.21.

Use the following method header:

public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years)

For example, futureInvestmentValue(10000, 0.05/12, 5) returns 12833.59.


Program

import java.util.*;
public class Investment {
    public static void main(String args[]) {
     Scanner in = new Scanner(System.in); 
    System.out.print("The amount invested: ");
  double amount = in.nextDouble();
  System.out.print("Annual interest rate: ");
double rate = in.nextDouble();
//int year = in.nextInt();
rate *= 0.01;
System.out.println("Years    FutureValue");
for(int i = 1; i <=30; i++) {
   int formatter = 19;
   if (i >= 10) 
   formatter = 18;
System.out.printf(i + "%"+formatter+".2f\n", futureInvestmentValue(amount, rate/12, i));
       }
}

 public static double futureInvestmentValue(double a, double ir, int years) {
return a * Math.pow(1 +ir, years * 12);
}

}

Result

The amount invested: 1000
Annual interest rate:9
 Years    FutureValue
1            1093.81
2            1196.41
3            1308.65
4            1431.41
5            1565.68
6            1712.55
7            1873.20
8            2048.92
9            2241.12
10           2451.36
11           2681.31
12           2932.84
13           3207.96
14           3508.89
15           3838.04
16           4198.08
17           4591.89
18           5022.64
19           5493.80
20           6009.15
21           6572.85
22           7189.43
23           7863.85
24           8601.53
25           9408.41
26          10290.99
27          11256.35
28          12312.28
29          13467.25

30          14730.58

No comments:

Post a Comment