Java Method Exercises: Compute the future investment value at a given interest rate for a specified number of years
Java Method: Exercise-8 with Solution
Write a Java method to compute the future investment value at a given interest rate for a specified number of years.
Sample data (Monthly compounded):
Input the investment amount: 1000
Input the rate of interest: 10
Input number of years: 5
Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise8 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the investment amount: ");
double investment = in.nextDouble();
System.out.print("Input the rate of interest: ");
double rate = in.nextDouble();
System.out.print("Input number of years: ");
int year = in.nextInt();
rate *= 0.01;
System.out.println("Years FutureValue");
for(int i = 1; i <= year; i++) {
int formatter = 19;
if (i >= 10) formatter = 18;
System.out.printf(i + "%"+formatter+".2f\n", futureInvestmentValue(investment, rate/12, i));
}
}
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years) {
return investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
}
}
Sample Output:
Input the investment amount: 1000 Input the rate of interest: 10 Input number of years: 5 Years FutureValue 1 1104.71 2 1220.39 3 1348.18 4 1489.35 5 1645.31
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java method to display the first 50 pentagonal numbers.
Next: Write a Java method to print characters between two characters (i.e. A to P ).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework