Question

P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as You win! and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct number. It should print an appropriate message, or You lose! when the user consumes all of his five tries. Hint: use srand(time(0)) and rand() from the libraraies <cstdlib> and <ctime

Using C++ language

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

Following is the answer:

#include<iostream>

#include<stdlib.h>

using namespace std;

int main()

{

int i;

int num;   

int n;

int count = 0;

num=rand()%100; //get random number

while(n!=num && count != 5){

cout<<"\nEnter Your choice: ";

cin>>n;

if(n<num){

cout<<"Number is less then Random number.";

count++;

}else if(n>num){

cout<<"Number is greater then Random number.";

count++;

}else{

cout<<"You Win!\n";

}

}

if(count == 5 && n != num){

cout<<"\nYou lose!\n";

}

return 0;

}

Output:

Enter Your choice: 12 Number is less then Random number Enter Your choice: 67 Number is less then Random number Enter Your choice: 89 Number is greater then Random number. Enter Your choice: 67 Number is less then Random number Enter Your choice: 56 Number is less then Random number You lose!

Add a comment
Know the answer?
Add Answer to:
Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...
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
  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • 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...

  • Write a C program that generates a random number between 0 and 50. Then, the program...

    Write a C program that generates a random number between 0 and 50. Then, the program prompts for guesses until the user is correct, or has made 10 wrong guesses. After each guess, the program indicates whether the guess was too high, too low, or correct. Once the round has ended, either by a correct guess or by using up the 10 guesses, the program displays the current status.

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • 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:...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number 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.

  • 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...

  • Write a program that prompts the user to enter a number within the range of 1...

    Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive. The program then generates a random number using the random class: If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number, If the users guess matches the random number tell them they win! If the users guess does not match the random number tell them they are wrong. Use a...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • (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...

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