The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.21.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.21 - Financial application: calculate future investment value<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_21 {<br /><br /> public static void main(String[] args) {<br /> <br /> // Display Program Information<br /> System.out.println("This Program Calculates The Future Investment Value"<br /> + " of Investment.n");<br /><br /> // create Scanner <br /> Scanner input = new Scanner(System.in);<br /><br /> // prompt user to enter details<br /> System.out.println("Enter investment amount:");<br /> double investmentAmount = input.nextDouble();<br /> System.out.println("Enter annual interest rate in percentage:");<br /> double monthlyInterestRate = input.nextDouble();<br /> System.out.println("Enter number of years:");<br /> double numberOfYears = input.nextDouble();<br /><br /> // calculate s using the formula futureInvestmentValue = <br /> // investmentAmount x (1 + monthlyInterestRate)^numberOfYears*12<br /> double futureInvestmentValue =<br /> investmentAmount * Math.pow((1 + (monthlyInterestRate / 1200)), (numberOfYears * 12));<br /> // format futureInvestmentValue to two decimal places<br /> futureInvestmentValue = (int) (futureInvestmentValue * 100) / 100.0;<br /><br /> // display the result<br /> System.out.println("Accumulated value is $" + futureInvestmentValue + "n");<br /> }<br />}<br /> |
Program output |
Click here to see other solutions to Introduction to Java Programming.