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

Write a program to play the \"guess a number game.\" Include the header files. I

ID: 3797904 • Letter: W

Question

Write a program to play the "guess a number game." Include the header files. In your program, seed the random number generator with time. srand(time(NULL)); Generate a random number between 1 and 20, using the following formula: Prompt the user to enter a guess. In a while loop tell the user if the guess is too high or too low. Have the user continue guessing until the correct guess is entered. Once the program is working, enclose in an outer while loop so that the user may play again if desired or may quit.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int main()
{

int choice;
do
{
printf(" 1.Play Game ");
printf("2.Exit ");
scanf("%d", &choice);

switch (choice)
{
case 1:
srand(time(NULL));
int r = rand() % (20+1-1) + 1;
bool correct = false;
int guess;
int counter = 0;
  
while(!correct)
{
printf("Guess my number! ");
scanf("%d", &guess);
getchar();
  
if (guess < r) {
printf("Your guess is too low. Guess again. ");
}
else if (guess > r) {
printf("Your guess is too high. Guess again. ");
}
else /* if (guess == r) */ {
printf("You guessed correctly in %d tries! Congratulations! ", counter);
correct = true;
}
counter++;
}
break;
case 2:printf("Quit ");
break;
  
default:printf("wrong choice.Enter Again");
break;
}

}while(choice !=2);

return 0;
}

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