Zenn
By: a guest | Jun 23rd, 2010 | Syntax:
None | Size: 1.35 KB | Hits: 255 | Expires: Never
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Start = 1, End = 100;
int RandomTarget(){
time_t seed;
time(&seed);
srand((unsigned int) seed);
return rand() % (Start - End + 1) + 1;
}
int main(){
printf("Hi, welcome to my lucky number game!\n"
"Please, guess your number and then input it in this screen, then enter.\n"
);
char Continue = 'Y';
while(Continue == 'Y' || Continue == 'y'){
int Target = RandomTarget(), Count = 0, Answer = -1;
Start = 1; End = 100;
while(Answer != Target){
while(Answer < Start || Answer > End){
printf("Please input your number in range [%d, %d]: ", Start, End);
scanf(" %d", &Answer);
}
Count++;
if(Answer > Target){
printf("It's too much.\n");
End = Answer-1;
}
else if(Answer < Target){
printf("It's too less.\n");
Start = Answer+1;
}
else{
printf("Congrats! You win, the correct number is \"%d\"\n", Target);
printf("And you guessed %d times\n", Count);
}
}
printf("Do you want to continue? (Y yes, N no): "); scanf(" %c", &Continue);
}
return 0;
}