-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1_numberGuessingGame.cpp
More file actions
35 lines (26 loc) · 1.14 KB
/
task1_numberGuessingGame.cpp
File metadata and controls
35 lines (26 loc) · 1.14 KB
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
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand((unsigned) time(NULL)); // Provided a seed value to generate random number
int minLimit, maxLimit;
cout << "Enter minimum and maximum limits respectively: " << endl;
cin >> minLimit >> maxLimit; // Taking minimum and maximum limits to generate random number
int range = maxLimit - minLimit + 1; // Defining range
int random = minLimit + (rand() % range); // Generating random number between range
int guess;
cout << "Guess the random number: " << endl;
cin >> guess; // Taking a number from a user as a guess
while (guess != random) {
if (guess < random) {
cout << "Try again! Guess is too low." << endl;
} else {
cout << "Try again! Guess is too high." << endl;
}
cout << "Guess the random number: " << endl;
cin >> guess;
} // Code to deal with the situation when guessed number is not equal to random number
cout << "Congratulations! You guessed the number correctly." << endl; // Till here the number is guessed correctly.
return 0;
}