#include #include /* we need this to call functions srand and rand */ #include /* we need this to call function time */ /* pick_random returns a random number between 0 and high. * 0 and high, as well as any number in between, are possible * values for the result. */ int pick_random(int high) { int number; int result; number = rand(); result = number % (high + 1); return result; } int main() { int number; int tries = 1; int guess; int low; int high; srand(time(0)); number = pick_random(1000); /* the first random number is not as random as we would like, * so we call pick_random twice, to get a less predictable * result. */ number = pick_random(1000); while(1) { printf("number of tries: %d\n", tries); printf("enter your guess:\n"); scanf("%d", &guess); if (guess == number) { printf("congrats!!! number of tries = %d\n", tries); break; } else if (guess < number) { printf("the number is greater than %d\n", guess); } else { printf("the number is less than %d\n", guess); } printf("\n"); tries = tries + 1; } }