Write a program that generates a random number between 1 and 100 and asks the us
ID: 3695008 • Letter: W
Question
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. Then the program should display "Congratulations. You figured out my number."
You must use a do-while loop
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int r,a;
r=rand()%100+1;
do
{
cout<<"Guess Number";
cin>>a;
if(a>r)
cout<<"Too high. Try again";
else if(a<r)
cout<<"Too low. Try again";
else
{
cout<<"Congratulations. You figured out my number.";
break;
}
}while(1);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.