Deitel & Deitel, C How to Program 6th Edition, Exercise 5.31

/*5.31 (Coin Tossing) 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
of times each side of the coin appears. Print the results. The program should call a separate function
flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program
realistically simulates the coin tossing, then each side of the coin should appear approximately half
the time for a total of approximately 50 heads and 50 tails.]*/

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int main() {
int i, j=1, k=1;
srand(time(NULL));
for (i = 1; i <= 100; i++) {
if (rand() % 2 == 1)
printf("HEADS-%d\n", j++);
else
printf("TAILS-%d\n", k++);
}
return 0;
}

Comments

Popular posts from this blog

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.23 (Turtle Graphics)

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.22 (Total Sales)

Deitel & Deitel, C How to Program 6th Edition, Exercise 5.47 (Craps Game Modification)