0% found this document useful (0 votes)
12 views1 page

guess the number

Uploaded by

shafibutt029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

guess the number

Uploaded by

shafibutt029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

#include <cstdlib> // For rand() and srand()


#include <ctime> // For time()

using namespace std;

int main() {
// Initialize random seed based on current time
srand(static_cast<unsigned int>(time(0)));

// Generate a random number between 1 and 100


int number = rand() % 100 + 1;
int guess;
int attempts = 0;

cout << "Welcome to 'Guess the Number' game!" << endl;


cout << "I have selected a number between 1 and 100. Try to guess it!" << endl;

// Keep asking the user for guesses until they get it right
do {
cout << "Enter your guess: ";
cin >> guess;
attempts++;

if (guess < number) {


cout << "Too low! Try again." << endl;
} else if (guess > number) {
cout << "Too high! Try again." << endl;
} else {
cout << "Congratulations! You guessed the number in " << attempts << "
attempts!" << endl;
}

} while (guess != number); // Continue the loop until the correct guess

return 0;
}

You might also like