Random number

In this program, we use the random number function to make the user guess a number between 1 and 100.

captura q.PNGCaptura2.PNG

 

If you want to make improvements or modifications, here is the code for you to copy and paste it anywhere.

# include <iostream>
# include <cmath>
# include <cstdlib>
using namespace std;
int randomnumber (){
int random;
random = rand()%100+1;
return random;
}
void hints (int number, int random){
if (number==random){
cout <<“You did it!!”<<endl;
}
else{
if(number<random){
cout<< “Too low”<<endl;
}
else if (number> random){
cout<< “Too high”<<endl;
}
else if(number<0 && numus>100){
cout << “Try whole numbers between 1 and 100″<<endl;
}
}
}
int main (){
int num, secret, counter=0;
char op;
cout<<“You must guess a number between 1 and 100″<<endl;
secret=randomnumber();
counter = 0;
do{
if (counter==0) {
cout<<“Guess the number”<<endl;
}
else {
cout<<“Guess again”<<endl;
}
cin>> num;
hints(num,secret);
contador++;
} while (num!= secret);
cout<<“You used “<<counter<<” tries”<<endl;
}

 

Leave a comment