72.6k views
1 vote
Suppose you find a magic $1.00 coin. Its magic power is as follows: as each day passes, you get an additional dollar plus half of what you already had (it appears by the window somehow).

Write a method called getRichQuick (no input is necessary for this method) that prints the first n days while your total is less than $1,000,000 dollars. In other words, how many days does it take for you to earn $1,000,000?
Your program should calculate these numbers and print the following output:
Day 1: $1
Day 2: $1 + ($1 + .50) = $2.50
Day 3: $2.50 + ($1 + 1.25) = $4.75
...
Day N: $X + ($1 + Y) ≥ $1000000

1 Answer

2 votes

Answer:

See explaination

Step-by-step explanation:

public class QuickRich {

static void getRichQuick() {

double amount = 1;

int day = 1;

System.out.println("Day 1: $1");

while (amount < 1000000) {

day++;

if(amount + 1 + (amount/2) < 1000000)

System.out.printf("Day %d: $%.2f + ($1 + %.2f) = $%.2f\\", day, amount, amount/2, amount+(amount/2)+1);

else

System.out.printf("Day %d: $%.2f + ($1 + %.2f) >= $1000000\\", day, amount, amount/2);

amount += (1 + (amount/2));

}

}

public static void main(String[] args) {

getRichQuick();

}

}

User MPlanchard
by
6.1k points