Question

Write a C program that guesses a number. The program should generate a random number from...

Write a C program that guesses a number. The program should generate a random number from 10-100. The program then prints the following:

A random number in the range has been generated by the program(Do not print the number). Can you guess what the number is? Please put in your first guess.

The player then types a first guess and the program will respond with one of the following options:

1. Excellent! You got it!

2. Too low (wrong). Try again.

3. Too high (wrong). Try again.

If the player's guess is incorrect, the program should loop until the player finally gets the number correct. The program should keep telling the player "too high" or "too low" to help the player "zero in" on the correct answer, The searching technique employed in the problem is called binary search.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for the given question

#include<stdio.h>

#include<math.h>

int main(){

int r=10+rand()%90; //generate a random number between 10 to 100

int flag=0; // to know if the player has reached the number

int x; // to store user guess

while(flag==0){

scanf("%d",&x);

if(r==x){

flag=1; //to get out of the loop

printf("Excellent! You got it!\n");

}

else if(r>x)

printf("Too low (wrong). Try again.\n");

else

printf("Too high (wrong). Try again.\n");

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a C program that guesses a number. The program should generate a random number from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT