Guest
Public paste!

Zenn

By: a guest | Jun 23rd, 2010 | Syntax: None | Size: 1.35 KB | Hits: 255 | Expires: Never
Copy text to clipboard
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int Start = 1, End = 100;
  5. int RandomTarget(){
  6.     time_t seed;
  7.     time(&seed);
  8.     srand((unsigned int) seed);
  9.     return rand() % (Start - End + 1) + 1;
  10. }
  11. int main(){
  12.     printf("Hi, welcome to my lucky number game!\n"
  13.             "Please, guess your number and then input it in this screen, then enter.\n"
  14.     );
  15.     char Continue = 'Y';
  16.     while(Continue == 'Y' || Continue == 'y'){
  17.         int Target = RandomTarget(), Count = 0, Answer = -1;
  18.         Start = 1; End = 100;
  19.         while(Answer != Target){
  20.             while(Answer < Start || Answer > End){
  21.                 printf("Please input your number in range [%d, %d]: ", Start, End);
  22.                 scanf(" %d", &Answer);
  23.             }
  24.             Count++;            
  25.             if(Answer > Target){
  26.                 printf("It's too much.\n");
  27.                 End = Answer-1;
  28.             }
  29.             else if(Answer < Target){
  30.                 printf("It's too less.\n");    
  31.                 Start = Answer+1;
  32.             }
  33.             else{
  34.                 printf("Congrats! You win, the correct number is \"%d\"\n", Target);
  35.                 printf("And you guessed %d times\n", Count);
  36.             }
  37.         }
  38.         printf("Do you want to continue? (Y yes, N no): "); scanf(" %c", &Continue);
  39.     }
  40.     return 0;
  41. }