Question

In: Computer Science

Write a program to toss a coin multiple times. Ask the user for how many times...

Write a program to toss a coin multiple times. Ask the user for how many times to toss the coin and then display the percentage of heads and tails (do not display each toss, just the totals). JAVA

Solutions

Expert Solution

JAVA Program:

import java.io.*;
import java.util.*;

class Toss {
   public static void main (String[] args) {
   int count,headcount=0,tailcount=0,toss;
       System.out.print("Enter the number of times to toss the coin: ");
       Scanner sc = new Scanner(System.in);
       count = sc.nextInt();
       Random rand = new Random();
       //0 -- head 1 -- tail
       for(int i=0;i<count;i++){
       toss = rand.nextInt(2);
       if(toss==0)
       headcount++;
       else
       tailcount++;
       }
       System.out.println("Total head count is "+headcount);
       System.out.println("Total tail count is "+tailcount);
       System.out.printf("Percentage of heads is %.2f\n",(float)headcount/(float)count);
       System.out.printf("Percentage of tails is %.2f",(float)tailcount/(float)count);
   }
}

Output:


Related Solutions

Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
How many ways can a person toss a coin 9 times so that the number of...
How many ways can a person toss a coin 9 times so that the number of tails is between 5 and 7 inclusive?
How many ways can a person toss a coin 7 times so that the number of...
How many ways can a person toss a coin 7 times so that the number of heads is between 4 and 6 inclusive?
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
Write a Java program that will first ask the user how many grades they want to...
Write a Java program that will first ask the user how many grades they want to enter. Then use a do…while loop to populate an array of that size with grades entered by the user. Then sort the array. In a for loop read through that array, display the grades and total the grades. After the loop, calculate the average of those grades and display that average. Specifications Prompt the user for the number of grades they would like to...
Write a program using C++ that ask the user for the flowing: How many shares to...
Write a program using C++ that ask the user for the flowing: How many shares to be bought: The price per share: Percent commission for the broker for each transaction: Average annual return as of percentage: The program should calculate and display the following: The amount paid for the stock alone (without the commission) The amount of the commissionThe total amount of paid (the payment for stock plus the commission) After TEN years, your shares will worth:
Write a python program that will ask the user to enter as many positive and negative...
Write a python program that will ask the user to enter as many positive and negative numbers and this process will only stop when the last number entered is -999. Use a while loop for this purpose. Your program should compute three sums; the sum of all numbers, sum of all positive numbers, and the sum of all negative numbers and display them. It should also display the count of all numbers, count of all positive numbers, and count of...
1. If you were to toss a fair coin 5 times, how many different possible sequences...
1. If you were to toss a fair coin 5 times, how many different possible sequences of flips would there be? 2. Suppose that employees at a large company are assigned an ID number which contains 5 numbers and 2 letters. How many possible combinations are there in this system?
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT