diff --git a/train/c/main.c b/train/c/main.c new file mode 100644 index 0000000..8185fed --- /dev/null +++ b/train/c/main.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include + +int main() { + while (true) { + srand(time(0)); + printf("Train\n"); + printf("Creative Computing Morristown, New Jersey\n"); + printf("Time - Speed Distance Exercise:"); + int car = (rand() %(65-40+1))+40; + int distance = (rand() %(20-5+1))+5; + int train = (rand() %(39-20+1))+20; + printf(" A car traveling %d MPH can make a certain trip in %d hours less than a train traveling at %d MPH.\n", car, distance, train); + while (true) { + char input[5]; + printf("\nHow long does a trip take by car "); + fgets(input, 5, stdin); + printf(input); + double answer = atof(input); + int car_time = distance * train / (car - train); + int percent = abs(((car_time)-answer)*100/answer)+.5; + if (percent > 5) { + printf("Sorry, you were off by %d percent\n", percent); + printf("Correct answer is %d hours\n", car_time); + } else { + printf("Good!, Answer within %d percent\n", percent); + printf("Another Problem? (yes or no)"); + char newgame[5]; + fgets(newgame, 5,stdin); + if (strcmp("yes", newgame)) { + printf("!"); + break; + } + return 1; + } + continue; + } + } +} \ No newline at end of file