Flip a Coin You will write a program that allows the user to

\"Flip a Coin!\" You will write a program that allows the user to simulate a coin flip. Note the following line of code: (Math.random()*2) +1; This line generates a random number (a float) between 1 and 2. If you wanted to simulate somebody rolling a die, you could store the value of this variable as: var coinFlip = (Math.random()*2) +1; (Be sure to include all the parentheses in this line of code.) Reminder - Of course, you’ll need to convert this to an integer….. Have a button that says: \"Flip the Coin!\" If the coin flip is a 1, then output \"Heads\". If the coin flip is a 2, then output \'Tails\". Use a basic if-else block for this. (Do not use two separate if statements). You can output the result to an alert box.

Solution

Writing the code in C as given you need if else blocks in the progarm

#include<stdio.h>

#include<math.h>

int main()

{

int choice = (int) (Math.random() * 2) + 1;

// Math. random will generate random float value between 0 and 1, hence the function will return random float value between 0 and 2, when type casted to int, it will return either 0 or 1

if(choice==1)

printf(\"Heads\ \");

window.alert(Heads);

else

printf(\"Tails\ \");

window.alert(Tails);

return 0;

}

\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site