Computer guess the number ...
Quote:
It is up to the computer to guess in seven tries the number the user is thinking of. On each move, the user must indicate whether the number tried is higher or lower than the number he/she thought of. Build your program so that the computer always wins.
My code:
Code:
#include <time.h>
#include<stdio.h>
#include <stdlib.h>
int main(void) {
int numberGuess = 0;
int low = 1;
int high = 100;
int computerGuess = 0;
printf("Enter a number, 1 - 100: ");
scanf("%d", &numberGuess);
while (computerGuess != numberGuess)
{
computerGuess = ((high - low)/2 + low);
printf("%d ", computerGuess);
if (numberGuess > computerGuess)
{
printf("Your guess was to low \n");
low = computerGuess+1;
}
else if (numberGuess < computerGuess)
{
printf("Your guess was to high \n");
high = computerGuess-1;
}
else if (numberGuess == computerGuess)
{
printf("Yess!! you got it!\n");
}
}
return 0;
}