Lab 6.2 Functions which return values Write a function named cointoss that simul
ID: 662959 • Letter: L
Question
Lab 6.2 Functions which return values
Write a function named cointoss that simulates the tossing of a coin.
When you call the function, it should generate a random number in the range of 1 through 2.
If the random number is 1, the function should display "heads".
If the random number is 2, the function should display "tails".
Demonstrate the function in a program that asks the user how many times the coin should be tossed, and then simulates tossing the coin that number of times.
Report the total number of heads and tails.
Explanation / Answer
int h=0;
int t=0;
if(randomInt == 1) {
System.out.println("Heads");
h++;
}
else {
System.out.println("Tails");
t++;
}
}
System.out.println("Total number of heads are : "+ h);
System.out.println("Total number of tails are : "+ t);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.