In: Computer Science
Use C++ language
3. Ask the user to pick a number between 1 and 100. You (the program) should try and guess the number the user picked. The user can tell you if their number is higher or lower, but that is all. The program should run until the computer guesses correctly.
Answer 1:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter number: ";
cin>>n;
if(n!=10 && n%3!=0)
cout<<"You made a good choice: ";
return 0;
}
Answer 2:
#include <iostream>
using namespace std;
int main()
{
int n;
string str;
while(true){
cout<<"Enter how many number of guests are coming: ";
cin>>n;
if(n>0)
break;
cout<<"Number can't be negative: "<<endl;
}
for(int i=0;i<n;i++){
cout<<"Enter guest name: ";
cin>>str;
cout<<"Hello "<<str<<endl;
}
return 0;
}
Answer 3:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int getRandom(){
srand(time(0)); //seed random number generator
int num = rand() % 100 + 1; // random number between 1
and 100
return num;
}
int getNum(){
int guess;
cout << "Enter a guess between 1 and 100 : ";
cin >> guess;
return guess;
}
int getRes(int g,int n){
if(g>n)
return 1;
else if(g<n)
return -1;
else
return 0;
}
int main()
{
int num, guess, res = 0,ch=1;
while(ch){
num=getRandom();
cout << "Guess My Number Game\n\n";
do
{
//getting random num
guess=getNum();
//checking res
res=getRes(guess,num);
if (res > 0)
cout <<
"Too high!\n\n";
else if (res < 0)
cout <<
"Too low!\n\n";
else
cout <<
"\nCorrect! You got it\n";
} while (guess != num);
cout<<"Press 1 to play again..0 to exit:
";
cin>>ch;
}
return 0;
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me