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)
#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;
}
Comments
Leave a comment