Back to Challenge Set 8

Code Challenge: Penny Doubling

Difficulty  

divider

The Brief

What they asked for
My grandpa says he would rather have a penny that doubles
every day than a million dollars right now. I think he is
nuts. Start with one penny on day 1, double it every day,
and show me how much there is each day. Stop on the first
day it is a million dollars or more, and tell me which day
that was.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Before you write anything, guess. How many days do you think it takes? Write your guess at the top of your requirements list. Most people are wildly off.
  • You do not know how many days it will take — that is the question. So the loop keeps going while the money is still under a million, and that is exactly what a while is for.
  • Money has cents, so the amount is a double. Show it with :F2, the way Activity 1.9 did.
  • You need two things changing every pass: the money doubles, and the day goes up by one. If the day number at the end looks wrong, one of those two is missing.
  • If it will not stop, press Ctrl + C.

Where to Start

Only one-star challenges have this section. If you already know how to begin, skip it — and when you move up to two stars, working out the order yourself is part of the job.

  1. Make a double for the money, starting at 0.01, and an int for the day, starting at 1.
  2. Show day 1 before the loop starts.
  3. Write a loop that keeps going while the money is under 1000000.
  4. Inside the loop: double the money, add one to the day, then show the new day.
  5. After the loop ends, say which day it was.

Sample Output

Sample Output
Day 1: $0.01
Day 2: $0.02
Day 3: $0.04
Day 4: $0.08
...
Day 27: $671088.64
Day 28: $1342177.28
A millionaire on day 28!

Your program prints every day — the sample skips the middle to save space. Check the last day your program prints matches the day it announces, and compare the answer with the guess you wrote down. Would you take grandpa's deal?


Submit

Your requirements list, including your guess, and your program file, named cc-penny-doubling.

Commence Challenge