Answer to Question #325249 in Algorithms for KeKo

Question #325249

Create a program that will prompt the user to enter a number in the range 1-100 to find my favourite number. If the number is less than favourite number, print “Too low...guess again: ”. If the number is greater than favourite number, print “Too high...guess again: ”. Ask again and exit until the number entered is the correct favourite number. in C++ program (while loop)


1
Expert's answer
2022-04-07T07:30:28-0400
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(nullptr));
    int favourite = rand() % 100 + 1;
    int guess = 0;

    cout << "Guess my favourite number (1-100):  ";
    while (guess != favourite) {
        cin >> guess;
        if (guess < favourite) {
            cout << "Too low...guess again: ";
        }
        else if (guess > favourite) {
            cout << "Too high...guess again: ";
        }
    }
    cout << "You are right!";

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS