Back to Challenge Set 7

Code Challenge: Coin Flip

Difficulty  

divider

The Brief

What they asked for
Settle an argument for me. Flip a coin and tell me heads or
tails — I don't want to see a number, I want to see a
word.

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


Notes

  • There is no input here at all — a coin flip does not ask the person anything first.
  • Random.Shared.Next(0, 2) gives you a whole number from 0 to 1. That is two outcomes, which is exactly a coin.
  • The brief wants a word, not a number. Turn the 0 or 1 into “Heads” or “Tails” with a single if / else.
  • This is the gentler cousin of Magic 8 Ball in this same set — two outcomes instead of eight, and no chain to write.

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. Store the result of Random.Shared.Next(0, 2) in an int.
  2. Write an if that checks whether it is 0.
  3. Print “Heads” in that case and “Tails” in the else.
  4. Run it several times. You should see both words show up, in no particular order.

Sample Output

One Possible Run
Heads

Your program picks randomly, so this is only one possible run — “Tails” is just as correct. What matters is that both words are reachable and nothing else ever prints.


Submit

Your requirements list and your program file, named cc-coin-flip.

Commence Challenge