Question

Write a program to play "guess my number". The program should generate a random number between...

Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE.

======================
Sample Input / Output:
======================
-------------------------------------
Sample 1: User incorrectly guesses 59
-------------------------------------
Guess a number between 0 and 100 and enter it here: 59
You guessed it wrong :( You should have gone higher.

-------------------------------------
Sample 2: User correctly guesses 43
-------------------------------------
Guess a number between 0 and 100 and enter it here: 43
Yes - that is correct :)

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

NOTE:- It is very hard to get a YES in this program. So the output with YES statement is not provided.

code:-

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


int main(){
   int guess;                           //variable to store gussed number from user
   int randomNumber;                   //variable to store random number generated by computer
   printf("Guess a number between 0 and 100 and enter it here: ");   //asking user to input gussed number
   scanf("%d",&guess);                   //input from user
   randomNumber = rand() % 100;       //generating random number

   while(randomNumber == 0){           // the generated number must be between 0 and 100 , the loop will exit only when the random number is not 0
       randomNumber = rand() % 100;   //generating random number
   }

   if(guess > randomNumber){           //if the user guessed wrong and the guessed number is greater than generated number
       printf("You guessed it wrong :( You should have gone lower.\n");
   }
   if(guess < randomNumber){           //if the user guessed wrong and the guessed number is less than generated number
       printf("You guessed it wrong :( You should have gone higher.\n");
   }

   if(guess == randomNumber){           //if the user guessed correct
       printf("Yes - that is correct :)\n");
   }

// printf("guessed number:%d %d ", guess, randomNumber);   //printing gussed number and the computer generated number for testing


   return 0;
}

output:-

C:\Users\rakesh\Desktop\che>gcc guessMyNumber.c

C:\Users\rakesh\Desktop\che>a
Guess a number between 0 and 100 and enter it here: 59
You guessed it wrong :( You should have gone lower.

C:\Users\rakesh\Desktop\che>gcc guessMyNumber.c

C:\Users\rakesh\Desktop\che>a
Guess a number between 0 and 100 and enter it here: 34
You guessed it wrong :( You should have gone lower.

C:\Users\rakesh\Desktop\che>

Add a comment
Know the answer?
Add Answer to:
Write a program to play "guess my number". The program should generate a random number between...
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
  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one...

    IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one and 100. Then it will ask the user to make a guess as to what the number is. If the guess is incorrect, but is within the range of 1 to 100, the user will be told if it is higher or lower and then asked to guess again. If the guess was outside the valid range or not readable by Scanner class, a...

  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • python Exercise: ex9.py Write a program that plays a number and you guess the number. The...

    python Exercise: ex9.py Write a program that plays a number and you guess the number. The program helps you with a response 1t the number you guessed is higher or lower than the mystery number. To generate a random number, we need to import some routines. guessing game. It generates a random mystery import random mysteryNumber random.randint (1, 100) The random.randint (1, 100) creates a random integer between 1 and 100. Here is a sample output: Let's play the guessing...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

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
ADVERTISEMENT