forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguessTheNumber.cpp
46 lines (34 loc) · 962 Bytes
/
guessTheNumber.cpp
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
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
int main(){
int start, end;
cout << "# Welcome to the Guess The Number Game #" << endl;
cout << "Choose your number range" << endl;
cout << "From : ";
cin >> start;
cout << "To : ";
cin >> end;
// check if the range (start == end)
if(start == end){
cout << "Imagine playing guess the number from " << start << " to " << end;
return 0;
}
srand((int)time(0));
int randnum = (rand() % end) + start;
system ("CLS"); // clear console
int guess; int test = 1;
cout << "Guess the number : ";
cin >> guess;
while(guess != randnum){
if(guess > randnum){
cout << "The number is lower than your answer" << endl;
} else {
cout << "The number is higher than your answer" << endl;
}
cout << "Guess the number : ";
cin >> guess;
test++;
}
system ("CLS"); // clear console
cout << "Congratulations, the number is " << randnum << " you have tried " << test << " times";
}