Guess the number

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.

using namespace std;
int main(){
	int n;
	unsigned s = time(NULL);
	int num = rand()%100+1;
	int count=0;
	cout<<"Guess the Number (1-100)"<<endl;
		cout<<"Guess : ";
		if(num==n) break;
		else if(n>num) cout<<"Your guess is high"<<endl;
		else cout<<"Your guess is low"<<endl;
	cout<<"The number is "<<num<<endl;
	cout<<"You got it in "<<count<<" guesses"<<endl;
	return 0;
output of the above program. guess the number
output of the above program. guess the number

Leave a Reply