-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrycode.cpp
More file actions
29 lines (24 loc) · 784 Bytes
/
trycode.cpp
File metadata and controls
29 lines (24 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // Seed for random number generation
int secretNumber = rand() % 100 + 1; // Random number between 1 and 100
int guess;
int attempts = 0;
cout << "🎮 Welcome to the Number Guessing Game!" << endl;
cout << "Guess the number (between 1 and 100): ";
do {
cin >> guess;
attempts++;
if (guess > secretNumber) {
cout << "Too high! Try again: ";
} else if (guess < secretNumber) {
cout << "Too low! Try again: ";
} else {
cout << "🎉 Congratulations! You guessed it in " << attempts << " attempts." << endl;
}
} while (guess != secretNumber);
return 0;
}