From 1a600f3a13c51961e44784e8c13156cdf4bb5c51 Mon Sep 17 00:00:00 2001 From: BBaoVanC Date: Thu, 19 Nov 2020 11:16:05 -0600 Subject: [PATCH] Add coinflip.c --- coinflip.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 coinflip.c diff --git a/coinflip.c b/coinflip.c new file mode 100644 index 0000000..376072a --- /dev/null +++ b/coinflip.c @@ -0,0 +1,34 @@ +#include +#include +#include + +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); + long int flips = strtol(rawInput, NULL, 0); + 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; +}