The completed program is given below.
In completing the program,
be sure that rate
is correct.
The user is supposed to enter percent interest,
for example 6 for six percent.
The program needs to convert that to a rate such as 0.06 to work correctly.
import java.util.Scanner; class DollarsAfterForty { public static void main( String[] args ) { double dollars = 1000.00 ; double rate; int year = 1 ; // get the interest rate from the user Scanner scan = new Scanner( System.in ); System.out.print("Enter the interest rate in percent: "); rate = scan.nextDouble()/100.0 ; while ( year <= 40 ) { // add another year's interest dollars = dollars + dollars*rate ; // add in this year's contribution dollars = dollars + 1000 ; year = year + 1 ; } System.out.println("After 40 years at " + rate*100 + " percent interest you will have " + dollars + " dollars" ); } }
Here is an example run:
C:\users\default\JavaLessons\chap19>java DollarsAfterForty Enter the interest rate in percent: 11 After 40 years at 11.0 percent interest you will have 646826.9337201559 dollars C:\users\default\JavaLessons\chap19>java DollarsAfterForty Enter the interest rate in percent: 12 After 40 years at 12.0 percent interest you will have 860142.3907860613 dollars C:\users\default\JavaLessons\chap19>java DollarsAfterForty Enter the interest rate in percent: 13 After 40 years at 13.0 percent interest you will have 1146485.7949682677 dollars
It looks like the interest rate should be somewhere between 12 and 13 percent.
In the above dialog, the user keeps trying interest rates until one is found where the account reaches the million dollar goal. In terms of computer programming, what is the user doing?