Write a game of guessing the number in C++. The computer should store the number between 1 and 100. And the user should guess the number. Print the number of guesses used by the user.

In this program, we use srand(seed), to generate a new random number each time. Then rand() is used to get the random number. To bring the number into range 1-100, we get the remainder by dividing 100 and adding 1. So the number ranges from 1 to 100.

```
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main(){
int n;
unsigned s = time(NULL);
srand(s);
int num = rand()%100+1;
int count=0;
cout<<"Guess the Number (1-100)"<<endl;
do{
cout<<"Guess : ";
cin>>n;
count++;
if(num==n) break;
else if(n>num) cout<<"Your guess is high"<<endl;
else cout<<"Your guess is low"<<endl;
}while(true);
cout<<"The number is "<<num<<endl;
cout<<"You got it in "<<count<<" guesses"<<endl;
return 0;
}
```

## One Reply to “Guess the number”