Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C language program that allows a user to guess a random number between 1

ID: 3840672 • Letter: W

Question

Write a C language program that allows a user to guess a random number between 1 and 1000. The function rand() from the header file can be used for this purpose. Before using the function, the random number generator must be provided a “seed” value to set it up. If the same seed value is supplied each time the program is run, the results of the random number generator will come out in the same order each time. A commonly used seed value is derived from the system clock, because the seconds count changes frequently enough to provide a different seed value each time we run the program. Include this in the beginning of your main program to seed the RNG: srand(time(NULL)); To use the time function, must be included. The rand() function returns a random integer between 0 and 32767. You will need to adjust the result to be between 1 and 1000 (think about how we might do this). Use a while loop to keep looping whenever the user guesses incorrectly. The loop should terminate when the user guesses the number correctly. Your program should count and print the number of guesses by the user. Each time the user guesses a number, you should print one of the following messages: Good guess. Too low, try again. Too high, try again. Sample Output: Guess my number 500 Too low, try again. Guesses: 1 750 Too high, try again. Guesses: 2 625 Too low, try again. Guesses: 3 675 Good guess. Guesses: 4

Explanation / Answer

Here is your code below: -

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main() {
   srand(time(NULL));
   int randomnumber;
randomnumber = rand() % 1000 +1; //generating random number
   int guessed = 0; //guessed flag
   int count = 0;
   int guess;
  
   printf("Guess My Number : ");
   while(guessed == 0) { //stop if guessed right
       count++;
       scanf("%d",&guess);
       if(guess > randomnumber) { // if guessed number is greater than generated number
           printf("Too High , Try Again. Guesses : %d ",count);
       } else if(guess < randomnumber) {// if guessed number is less than generated number
           printf("Too Low , Try Again. Guesses : %d ",count);
       } else {// if guessed number is equal to generated number
           printf("Good Guess. Guesses : %d ",count);
           guessed = 1;
       }
   }
   return 0;
}

Sample Run: -

Guess My Number : 200
Too Low , Try Again. Guesses : 1 300
Too Low , Try Again. Guesses : 2 400
Too Low , Try Again. Guesses : 3 500
Too High , Try Again. Guesses : 4 450
Too High , Try Again. Guesses : 5 425
Too Low , Try Again. Guesses : 6 430
Too Low , Try Again. Guesses : 7 435
Too Low , Try Again. Guesses : 8 440
Too High , Try Again. Guesses : 9 436
Too Low , Try Again. Guesses : 10 437
Good Guess. Guesses : 11
--------------------------------
Process exited after 51.11 seconds with return value 0
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote