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

As we continue with our study of programming fundamentals, here is a short extra

ID: 3919548 • Letter: A

Question

As we continue with our study of programming fundamentals, here is a short extra credit programming challenge involving repetition control structures. To be specific, the program specifications below and the algorithm you develop and write will involve the set-up and use of both while and do-while loops. Random Number Guessing Game Write a program that generates a random number between 5 and 15 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 while keeping a count of the number of guesses the user makes until the user correctly guesses the random number. Then the program should display the number of guesses along with the following message Congratulations. You figured out my number. Suggest that you also give the user the opportunity to play the game again or quit.

Explanation / Answer

Hi, I have done in C++ because you have not mentioned any language. Let me know if you need need in any other language or you have any doubt in this.

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

int num, guess, tries = 0;

int min=5,max=15;

srand(time(0)); //seed random number generator

num = rand() % (max-min + 1) + min; // random number between 5 and 15

cout << "Guess My Number Game ";

do

{

cout << "Enter a guess between 5 and 15 : ";

cin>> guess;

tries++;

if (guess > num)

cout << "Too high! ";

else if (guess < num)

cout << "Too low! ";

else

cout << " Congratulations. You figured out my number in ." << tries << " guesses! ";

} while (guess != num);

cin.ignore();

cin.get();

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