In: Computer Science
The First National Bank of Parkville recently opened up a new “So You Want to Be a Millionaire” savings account. The new account works as follows:
Write a java program that prompts the user for a starting balance and then prints the number of years it takes to reach $100,000 and also the number of years it takes to reach $1,000,000.
Sample session:
Enter starting balance: 10000
It takes 4 years to reach $100,000.
It takes 7 years to reach $1,000,000.
/*Java program that prompts user to enter the
starting balance and then find a number of years to reach 100000$
and number of years to reach 1000000$
*/
//Millionaire.java
import java.util.Scanner;
public class Millionaire
{
public static void main(String[] args)
{
//create an instance of Scanner
class
Scanner kboard=new
Scanner(System.in);
//declare variables
double balance=0;
double amount=0;
int year=0;
boolean run=true;
//set one lack
final double
ONE_LACK=1_000_00;
//set one million
final double
ONE_MILLION=1_000_000;
System.out.print("Enter starting
balance: ");
//read balance from keyboard
balance=Double.parseDouble(kboard.nextLine());
//set balance
amount=balance;
//Loop to reach one lack
while(amount<=ONE_LACK
&& run )
{
//double the
amount
amount=2*amount;
//increment the
year by 1
year++;
if(amount>=ONE_LACK)
//set run to false if amount reached
100000
run=false;
}
//print the years to reach one lack
dollars
System.out.println("It takes
"+year+" years to reach $"+ONE_LACK);
//Reset year to 0
year=0;
//Reset run to true
run=true;
//set balance to amount
amount=balance;
//Loop to reach one million
while(amount<=ONE_MILLION
&& run )
{
//double the
amount
amount=2*amount;
//increment the
year by 1
year++;
//set run to
false if amount reached 1000000
if(amount>=ONE_MILLION)
run=false;
}
//print the years to reach one
million dollars
System.out.println("It takes
"+year+" years to reach $"+ONE_MILLION);
}
}
--------------------------------------Sample
Run#---------------------------------------------------------------
