Absolutely NOT! That's 80 years without chocolate-chip cookies!
We obviously need to find a bank with an interest rate higher than five percent. Say that you are willing to wait 40 years for your million dollars. How high must the interest rate be?
One way to answer this question is to try out various interest rates until you find one that works. Here is a program that does that:
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 <= ) { // add another year's interest dollars = dollars + dollars * ; // 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" ); } }
This program does NOT use a result-controlled loop.
It uses a counting loop because the counter, year
,
is used to control the loop.
Fill in the blanks to complete the program.