← Back
Code Challenge 2.7 - Coin Flip Simulator
Objective
Your task is to create a program that simulates flipping a coin multiple times based on user input. The program will keep track of how many times heads and tails occur and display the counts and percentages at the end.
Skills to Practice
- Using loops to repeat actions based on user input.
- Generating random numbers to simulate coin flips.
- Counting occurrences and calculating percentages.
- Displaying results in a clear and concise format.
Challenge Tasks
- Create a console project named Challenge 2-7.
- Ask the user to enter the number of times they want to flip the coin.
- Use a loop to “flip” the coin the specified number of times.
- Keep track of the number of heads and tails.
- Calculate the percentage of heads and tails.
- Display the total number of heads and tails and their respective percentages.
Sample Output
Enter the number of times to flip the coin: 10 [Enter]
Flip 1: Heads
Flip 2: Tails
Flip 3: Heads
Flip 4: Heads
Flip 5: Tails
Flip 6: Heads
Flip 7: Tails
Flip 8: Tails
Flip 9: Heads
Flip 10: Heads
Results:
Heads: 6 (60%)
Tails: 4 (40%)