Developing a "guess the number" game in C?
-
Your program generates a random number (integer) in the range 1 to 1000. The program shows following to the user: “I have a number between 1 and 1000. Can you guess it? Please enter your guess.” The player then guesses. The program responds with one of the following: If the users guess is correct, program shows the following output 1. Excellent! You guessed the number with n tries! (Where n is the number of try e.g. 1,2,3…) Would you like to play again (y/n)? If the user enters a value that is less than the computer’s number (random number), the following output is shown 2. Too low. Try again. If the user enters a value that is greater than the computers number, the following output is shown 3. Too high. Try again. The program then asks for the next guess. When the user enters the correct answer, display "Excellent, you guessed the number with “n” tries!", and allow the user to choose whether to play again. Replace n with the number of tries user has taken to guess the number.
-
Answer:
here is most of it. you can add the part where it asks if you want to try again.... Untested code follows: #include <stdio.h> #include <time.h> int main(void){ int r,g,l,h; int tries; srand(time(0)); h=1000; l=1; r=rand()%h+l; tries=0; do{ printf("guess a number from %d to %d:",l,h); scanf("%d",&g); tries++; if(g<r){ l=g; printf("Too low.\n" } if(g>r){ h=g; printf("Too high.\n"); } if(g==r) printf("That is correct! In only %d tries",tries); }while(r!=g); return 0; }
roger at Yahoo! Answers Visit the source
Other answers
Obviously this is an assignment. I also got it in my cs lab. You need to include time.h library. Use srand(Time(NULL)) as a seed generator. number=rand()%1000+1 //gives you the random number of range. After that it just a matter ofwhile loop and if else.
drchanda q
include<stdio.h> #include<stdlib.h> main() { int i=0, N, ans; N=rand()%201; //in some libraries it can be rand(201) also. In some random(201); printf("Enter your guess 0-200\n"); while(1) { scanf("%d", &ans); if(ans ==N) { printf("Grand Success. Score=", 10-i); break;} else if(ans<N) printf("Your guess is low. Enter again"); else printf("Enter your guess is High. Enter Again\n"); i++; if(i==10) { printf("Next time better luck\n"); break; } } NOTE: check rand() or random function to be used.
James Bond
Related Q & A:
- How to arrange a set of number?Best solution by math.stackexchange.com
- How can I apply for a tax identification number in the Philippines?Best solution by Yahoo! Answers
- Does anyone know how id go about setting up a soccer management simulation game site to run as a business?Best solution by Yahoo! Answers
- How much does a GUESS wallet cost at Auckland International Airport?Best solution by Yahoo! Answers
- Is there a site where I can learn C++ easily?Best solution by Blurtit.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.