Thread: Computer guess the number ...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    9

    Computer guess the number ...

    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; 
    }
    Last edited by puctw; 03-04-2021 at 05:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game: Computer Guess My Number
    By Laythe in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2012, 11:25 PM
  2. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  3. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  4. Guess My Number (Need help)
    By dhardin in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2009, 12:59 PM
  5. can u guess my number?
    By hotsox in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 01:40 PM

Tags for this Thread