can you solve this c++ program please (codeblocks) Exercise #1: Guess a number W
ID: 3825502 • Letter: C
Question
can you solve this c++ program please (codeblocks)
Exercise #1: Guess a number Write a program that chooses a random integer between 0 and 99. The user tries to guess the integer in a minimum number of trials. For each trial, your program should indicate if the integer is lower, higher or cqual. At the end, the program displays thc number of trials done by the user. Sample input/ output Guess my chosen number Try a number: 50 The number is less than: 50 Try a number: 25 The number is less than: 25 Try a number: 12 The number is less than: 12 Try a number: 6 The number is more than: 6 Try a number: 8 The number is more than: 8 Try a number: 9 Yes, the number is: 9 got in 5 TrialsExplanation / Answer
Answer is given below:
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
int randomNumber;
srand(time(NULL));
randomNumber = rand() % 100 + 1;
int guessedNumber, guessCount=0;
do {
guessCount++ ;
cout << "Guess my chosen number Try a number: ";
cin >> guessedNumber;
if (randomNumber < guessedNumber){
cout << "The number is less than: "<< guessedNumber << endl;
}
else if (randomNumber > guessedNumber){
cout << "The number is more than: "<< guessedNumber << endl;
}
else{
cout << "Yes, the number is: " << guessedNumber << " got in "<<guessCount <<" Trials" << endl;
}
} while (guessedNumber != randomNumber);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.