c-practice/coinflip.c

35 lines
798 B
C
Raw Normal View History

2020-11-19 11:16:05 -06:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned) time(NULL));
printf("How many times would you like to flip the coin?\n");
printf("> ");
char rawInput[19];
fgets(rawInput, 19, stdin);
printf("rawInput = %s\n", rawInput);
unsigned long int flips = strtol(rawInput, NULL, 0);
2020-11-19 11:16:05 -06:00
printf("Ok, flipping %li times!\n", flips);
long int headsCount = 0;
long int tailsCount = 0;
int currentFlip;
for (int i = 0; i < flips; i++) {
currentFlip = rand() % 2;
//printf("Flip: %d\n", currentFlip);
if (currentFlip) {
headsCount++;
} else {
tailsCount++;
}
}
printf("Heads: %li\n", headsCount);
printf("Tails: %li\n", tailsCount);
return 0;
}